equality (1) | template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
|
---|---|
predicate (2) | template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred); |
Searches the range [first,last)
for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found.
在范围[first,last)之间寻找第一次出现的两个连续相等的元素,如果存在,则返回指向第一个元素迭代器,否则返回last.
例子:
#include <iostream> #include <algorithm> using namespace std; int main() { vector<int> vi{0,1,0,2,2,4,5,6}; auto it=adjacent_find(vi.begin(),vi.end()); if(it!=vi.end()) cout<<"some match"<<endl; else cout<<"mismacht"<<endl; cout<<"*it="<<*it<<endl; }运行截图:
operator==
(or using pred, in version (2)).如果使用==两个元素相等,那么就匹配。(第二种使用二元断言)
例子:
#include <iostream> #include <algorithm> #include <vector> using namespace std; bool isEqual(int i,int j){ if(i==j-1)//be care return true; else return false; } int main() { vector<int> vi{10,9,8,7,5,6}; auto it=adjacent_find(vi.begin(),vi.end(),isEqual); if(it!=vi.end()) cout<<"some match"<<endl; else cout<<"mismacht"<<endl; cout<<"*it="<<*it<<endl; }运行截图:
The behavior of this function template is equivalent to:
其行为类似如下:
|
|
Forward iterators to the initial and final positions of the searched sequence. The range used is [first,last)
, whichcontains all the elements between first and last, including the element pointed by first but not the elementpointed by last.
要搜索序列的标记范围,包含了fist到last之间的所有元素,包括first所指向的元素,但不包括last指向的元素。
bool
. The returned value indicates whether the elements are considered to match in the context of this function.This can either be a function pointer or a function object.
一个接受两个元素类型参数并返回一个bool值的二元函数。
该函数不应该修改其参数。
可以是一个指针或者函数对象。
[first,last)
.如果匹配返回匹配的指向第一个元素的迭代器,否则返回last.
例子:
#include <iostream> #include <algorithm> using namespace std; int main() { vector<int> vi{0,1,0,2,3,4,5,6}; auto it=adjacent_find(vi.begin(),vi.end()); if(it!=vi.end()) cout<<"some match"<<endl; else cout<<"mismacht"<<endl; cout<<"*it="<<*it<<endl; }运行截图:
|
|
Edit & Run
|
the first pair of repeated elements are: 30 the second pair of repeated elements are: 10 |
和first,last之间的距离线性相关,比较两个元素直到匹配出现。
[first,last)
are accessed (once at most).Note that invalid arguments cause undefined behavior.
如果任一元素比较或者迭代器操作抛出异常,则会抛出异常。
无效的参数导致未定义的行为。
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:[email protected]
2014-9-1
于GDUT
——————————————————————————————————————————————————————————————————