在线文档教程
C++
算法 | Algorithm

std::adjacent_find

STD::相邻[医]找出

Defined in header
template< class ForwardIt > ForwardIt adjacent_find( ForwardIt first, ForwardIt last (1)
template< class ExecutionPolicy, class ForwardIt > ForwardIt adjacent_find( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last (2)(since C++17)
template< class ForwardIt, class BinaryPredicate> ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p (3)
template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate> ForwardIt adjacent_find( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, BinaryPredicate p (4)(since C++17)

搜索范围[first, last)对于两个连续的相同元素。

1%29个元素的比较operator==...

使用给定的二进制谓词对3%29个元素进行比较。p...

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last-the range of elements to examine
policy-the execution policy to use. See execution policy for details.
p-binary predicate which returns ​true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b The signature does not need to have const &, but the function must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ​

类型要求

---。

返回值

第一对相同元素中的第一对元素的迭代器,即第一个迭代器。it使...*it == *(it+1)第一个版本或p(*it, *(it + 1)) != false第二个版本。

如果找不到这样的元素,last会被归还。

复杂性

1,3%29min((result-first)+1, (last-first)-1)谓词的应用程序result返回值。

2,4%29O(last-first)相应谓词的应用程序。

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<class ForwardIt>前邻[医]查找%28 Forwardit First,Forwardit持续%29{if%281==最后%29{返回最后;}Forwardit Next=First;++Next;对于%28;下%21=Lest;++Next,++First%29{if%28%2A第一==%2A下一个%29{先返回;}最后返回;}

第二版

模板<类向前,类二进制预测>向前相邻[医]查找%28 Forwardit First,Forwardit Lest,BinaryPredicate p%29{if%28first==最后%29{返回最后一个;}Forwardit下一步=先;++下一步;对于%28;下一个%21=最后;++Next,++Next,++第29%{if%28p%28%2A首先,%2A下一个%29%29{先返回;}最后返回;}

二次

#include <algorithm> #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 at: " << std::distance(v1.begin(), 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) << '\n'; } }

二次

产出:

二次

The first adjacent pair of equal elements at: 4 The last element in the non-decreasing subsequence is at: 7

二次

另见

uniqueremoves consecutive duplicate elements in a range (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/Nextance[医]找出