c++ - std::adjacent_find(last, last) 是否未定义?

标签 c++ algorithm language-lawyer

std::adjacent_find

searches the range [first, last) for two consecutive identical elements.

Return value

an iterator to the first of the first pair of identical elements, that is, the first iterator it such that *it == *(it+1) for the first version or p(*it, *(it + 1)) != false for the second version.

If no such elements are found, last is returned

但是,不清楚它应该如何处理范围 {last, last}。

cppreference 上的两种可能实现都有以下检查:

if (first == last) {
    return last;
}

std::adjacent_find(last, last) UB 或标准是否定义了它?

最佳答案

没有。它定义明确。

引用 C++ 标准草案 N4296,第 25.2.8/1 节:

Returns: The first iterator i such that both i and i + 1 are in the range [first,last) for which the following corresponding conditions hold: *i == *(i + 1), pred(*i, *(i + 1)) != false. Returns last if no such iterator is found.

因此,如果 first == last,则表示搜索范围为空,因此该方法应返回 last

CppReference 的检查非常正确。也是mentioned on CppReference那:

If no such elements are found, last is returned

关于c++ - std::adjacent_find(last, last) 是否未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396587/

相关文章:

java - C# 和 Java 规范在有符号整数溢出时是否阐明了相同的行为?

c++ - LNK2019案例如何解决?一切似乎都是正确的

java - 测试点是否在二维空间的线范围内

c++ - 如何使用 CreateFile API 打开分区?

algorithm - 在 Julia 中设置算法的时间限制

algorithm - 红黑树的直觉

c++ - 指向非法引用的成员的指针?

c++ - 嵌套类名在封闭类模板中使用时是否被视为当前实例化

c++ - 具有关联属性的 boost::hash_combine 的替代方案?

c++ - 类中成员函数声明的问题