adjacent_find [C++ Reference]
Keywords: stl,algorithm,adjacent_find
Date: 2009-10-09T00:51:43-0700

[[adjacent_find]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## adjacent_find

Syntax:

        #include <algorithm>
        forward_iterator adjacent_find( forward_iterator start, forward_iterator end );
        forward_iterator adjacent_find( forward_iterator start, forward_iterator end, BinPred pr );

The adjacent_find() function searches between start and end for two consecutive identical elements. If the binary predicate pr is specified, then it is used to test whether two elements are the same or not. The return value is an iterator that points to the first of the two elements that are found. If no matching elements are found, the returned iterator points to end. For 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:

       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);
         }
       }

       vector<int>::iterator result;
       result = adjacent_find( v1.begin(), v1.end() );

       if( result == v1.end() ) {
         cout << "Did not find adjacent elements in v1" << endl;
       }

       else {
         cout << "Found matching adjacent elements starting at " << *result <<
      endl;
       }

Related Topics: find, find_end, find_first_of, find_if, unique, unique_copy

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •