std::adjacent_find
From Cppreference
C++ Standard Library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Algorithm library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header
<algorithm> | ||
template< class ForwardIterator >
ForwardIterator adjacent_find( ForwardIterator first, ForwardIterator last ); | (1) | |
template< class ForwardIterator, BinaryPredicate p >
ForwardIterator adjacent_find( ForwardIterator first, ForwardIterator last, BinaryPredicate p ); | (2) | |
Searches the range [first, last) for two consecutive identical elements. The first version uses operator== to compare the elements, the second version uses the given binary predicate p.
Contents |
[edit] Parameters
first, last | - | the range of elements to examine | |||||||||
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:
The signature does not need to have const &, but the function must not modify the objects passed to it. |
[edit] Return value
an iterator to the first of the identical elements. If no such elements are found, last is returned
[edit] Equivalent function
First version: |
---|
template<class ForwardIterator> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last) { if (first == last) { return last; } ForwardIterator next = first; ++next; for (next != last; ++next, ++first) { if (*first == *next) { return first; } } return last; } |
Second version: |
template<class ForwardIterator, BinaryPredicate p> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate p) { if (first == last) { return last; } ForwardIterator next = first; ++next; for (next != last; ++next, ++first) { if (p(*first, *next)) { return first; } } return last; } |
[edit] Example
The following code creates a vector containing the integers between 0 and 10 with 7 appearing twice in a row. adjacent_find() is then used to find the location of the pair of 7's:
#include <algorithm> #include <iostream> int main() { std::vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i); // add a duplicate 7 into v1 if (i == 7) { v1.push_back(i); } } std::vector<int>::iterator result; result = std::adjacent_find(v1.begin(), v1.end()); if (result == v1.end()) { std::cout << "no matching adjacent elements"; } else { std::cout << "match at: " << std::distance(v1.begin(), result); } return 0; }
Output:
match at: 7
[edit] Complexity
linear in the distance between first and last
[edit] See also
| removes consecutive duplicate elements in a range (function template) |