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 orp(*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 bothi
andi + 1
are in the range[first,last)
for which the following corresponding conditions hold:*i == *(i + 1)
,pred(*i, *(i + 1)) != false
. Returnslast
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/