Protype of function is
template<class ForwardIterator>
ForwardIterator adjacent_find(
ForwardIterator _First,
ForwardIterator _Last
);
template<class ForwardIterator , class BinaryPredicate>
ForwardIterator adjacent_find(
ForwardIterator _First,
ForwardIterator _Last,
BinaryPredicate _Comp
);
One example, #include <algorithm>
#include <list>
#include <iostream>
using namespace std;
int main(){
list<int> iList;
iList.push_back(3);
iList.push_back(6);
iList.push_back(9);
iList.push_back(11);
iList.push_back(11);
iList.push_back(18);
iList.push_back(20);
iList.push_back(20);
list<int>::iterator iter;
for(iter = iList.begin(); iter != iList.end(); ++iter){
cout << *iter << " ";
}
cout << endl;
list<int>::iterator iResult = adjacent_find(iList.begin(), iList.end());
if (iResult != iList.end()){
cout << "链表中第一对相等的邻近元素为:" << endl;
cout << *iResult++ << endl;
cout << *iResult << endl;
}
iResult = adjacent_find(iList.begin(), iList.end(), [](int x, int y){return (x - y) % 2 == 0 ? 1 : 0;});
if (iResult != iList.end()){
cout << "First pair with same parity:" << endl;
cout << *iResult++ << endl;
cout << *iResult << endl;
}
return 0;
}
Note that *iResult++, first outputs *iResult, then add one to iResult.