Or login with:
#include <algorithm> template < class ForwardIterator > ForwardIterator adjacent_find( ForwardIterator first, ForwardIterator last ); template < class ForwardIterator , class BinaryPredicate > ForwardIterator adjacent_find( ForwardIterator first, ForwardIterator last, BinaryPredicate comp );Parameters:
| Parameter | Description |
|---|---|
| first | A forward iterator addressing the position of the first element in the range to be searched |
| last | A forward iterator addressing the position one past the final element in the range to be searched |
| comp | The binary predicate giving the condition to be satisfied by the values of the adjacent elements in the range being searched |
i such that i and i+1 are both valid iterators in [first, last), and such that *i == *(i+1). It returns last if no such iterator exists.
The second version of adjacent_find returns the first iterator i such that i and i+1 are both valid iterators in [first, last) and such that binary_pred (*i, *(i+1)) is true. It returns last if no such iterator exists.first == last then no comparison are performed; otherwise, at most (last - first) - 1 comparisons.#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = {1, 2, 3, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10}; vector<int> v(a, a+12); cout <<"\nHere are the values in the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; vector<int>::iterator p = adjacent_find(v.begin(), v.end()); if (p == v.end()) cout <<"\nThere are no, or no more, matching pairs."; else cout <<"\nFirst value of first matching pair is "<<*p<<"."; p = adjacent_find(p+1, v.end()); if (p == v.end()) cout <<"\nThere are no, or no more, matching pairs."; else cout <<"\nFirst value of next matching pair is "<<*p<<"."; p = adjacent_find(p+1, v.end()); if (p == v.end()) cout <<"\nThere are no, or no more, matching pairs."; else cout <<"\nFirst value of next matching pair is "<<*p<<"."; p = adjacent_find(p+1, v.end()); if (p == v.end()) cout <<"\nThere are no, or no more, matching pairs."; else cout <<"\nFirst value of next matching pair is "<<*p<<"."; return 0; }
You must login to leave a messge