std::adjacent_find
定义于头文件 <algorithm> |
||
template< class ForwardIt > ForwardIt adjacent_find( ForwardIt first, ForwardIt last ); |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt adjacent_find( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class ForwardIt, class BinaryPred > ForwardIt adjacent_find( ForwardIt first, ForwardIt last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class BinaryPred > ForwardIt adjacent_find( ExecutionPolicy&& policy, |
(4) | (since C++17) |
在范围 [
first,
last)
中搜索两个连续相等的元素。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(since C++20) |
内容 |
[编辑] 参数
first, last | - | 定义要检查的元素范围的迭代器对 |
policy | - | 要使用的执行策略 |
p | - | 二元谓词,如果元素应被视作相等则返回 true。 谓词函数的签名应等效于以下形式 bool pred(const Type1 &a, const Type2 &b); 虽然签名不需要具有 const &,但该函数不得修改传递给它的对象,并且必须能够接受类型(可能是 const) |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-BinaryPred 必须满足 BinaryPredicate 的要求。 |
[编辑] 返回值
指向第一对相同元素中第一个元素的迭代器,即第一个迭代器 it,使得对于 (1,2),*it == *(it + 1) 成立,或者对于 (3,4),p(*it, *(it + 1)) != false 成立。
如果未找到此类元素,则返回 last。
[编辑] 复杂度
给定 adjacent_find
的返回值 result,M 为 std::distance(first, result),N 为 std::distance(first, last)
[编辑] 异常
带有名为 ExecutionPolicy
的模板参数的重载按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
adjacent_find (1) |
---|
template<class ForwardIt> ForwardIt adjacent_find(ForwardIt first, ForwardIt last) { if (first == last) return last; ForwardIt next = first; ++next; for (; next != last; ++next, ++first) if (*first == *next) return first; return last; } |
adjacent_find (3) |
template<class ForwardIt, class BinaryPred> ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPred p) { if (first == last) return last; ForwardIt next = first; ++next; for (; next != last; ++next, ++first) if (p(*first, *next)) return first; return last; } |
[编辑] 示例
#include <algorithm> #include <functional> #include <iostream> #include <vector> int main() { std::vector<int> v1{0, 1, 2, 3, 40, 40, 41, 41, 5}; auto i1 = std::adjacent_find(v1.begin(), v1.end()); if (i1 == v1.end()) std::cout << "No matching adjacent elements\n"; else std::cout << "The first adjacent pair of equal elements is at " << std::distance(v1.begin(), i1) << ", *i1 = " << *i1 << '\n'; auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>()); if (i2 == v1.end()) std::cout << "The entire vector is sorted in ascending order\n"; else std::cout << "The last element in the non-decreasing subsequence is at " << std::distance(v1.begin(), i2) << ", *i2 = " << *i2 << '\n'; }
输出
The first adjacent pair of equal elements is at 4, *i1 = 40 The last element in the non-decreasing subsequence is at 7, *i2 = 41
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 240 | C++98 | 谓词被应用 std::find (first, last, value) - first 次 对于 (1,3),其中 value 从未定义 |
应用 std::min( (result - first) + 1, (last - first) - 1) 次 |
[编辑] 参见
移除范围中连续的重复元素 (函数模板) | |
(C++20) |
查找首两个相邻且相等(或满足给定谓词)的项 (算法函数对象) |