gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:10:52 30 4
gpt4 key购买 nike

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com