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); |
template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last) { if (first != last) { ForwardIterator next=first; ++next; while (next != last) { if (*first == *next) // or: if (pred(*first,*next)), for version (2) return first; ++first; ++next; } } return last; } |
// adjacent_find example #include <iostream> // std::cout #include <algorithm> // std::adjacent_find #include <vector> // std::vector bool myfunction (int i, int j) { return (i==j); } int main () { int myints[] = {5,20,5,30,30,20,10,10,20}; std::vector<int> myvector (myints,myints+8); std::vector<int>::iterator it; // using default comparison: it = std::adjacent_find (myvector.begin(), myvector.end()); if (it!=myvector.end()) std::cout << "the first pair of repeated elements are: " << *it << '\n'; //using predicate comparison: it = std::adjacent_find (++it, myvector.end(), myfunction); if (it!=myvector.end()) std::cout << "the second pair of repeated elements are: " << *it << '\n'; return 0; } |
unique | 删除范围内连续的重复项(函数模板) |
find | 在范围内查找值(函数模板) |
find_if | 在范围内查找元素(函数模板) |