cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Algorithms : adjacent_find
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Algorithms
algorithm:
· adjacent_find
· binary_search
· copy
· copy_backward
· count
· count_if
· equal
· equal_range
· fill
· fill_n
· find
· find_end
· find_first_of
· find_if
· for_each
· generate
· generate_n
· includes
· inplace_merge
· iter_swap
· lexicographical_compare
· lower_bound
· make_heap
· max
· max_element
· merge
· min
· min_element
· mismatch
· next_permutation
· nth_element
· partial_sort
· partial_sort_copy
· partition
· pop_heap
· prev_permutation
· push_heap
· random_shuffle
· remove
· remove_copy
· remove_copy_if
· remove_if
· replace
· replace_copy
· replace_copy_if
· replace_if
· reverse
· reverse_copy
· rotate
· rotate_copy
· search
· search_n
· set_difference
· set_intersection
· set_symmetric_difference
· set_union
· sort
· sort_heap
· stable_partition
· stable_sort
· swap
· swap_ranges
· transform
· unique
· unique_copy
· upper_bound

-

adjacent_find function template
template <class ForwardIterator>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last );

template <class ForwardIterator, class BinaryPredicate>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last,
                                   BynaryPredicate pred );
<algorithm>

Find equal adjacent elements in range

Searches the range [first,last) for the first occurrence of two consecutive equal elements, and returns an iterator to the first of these two elements.

The comparison between the consecutive elements is performed by either applying the == comparison operator, or the template parameter comp (for the second version) between them.

The behavior of this function template is equivalent to:

template <class ForwardIterator>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator next=first; ++next;
  if (first != last)
    while (next != last)
      if (*first++ == *next++)  // or: if (pred(*first++,*next++)), for the pred version
        return first;
  return last;
}

Parameters

first, last
Forward iterators to the initial and final positions of the searched sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
pred
Binary predicate taking two elements as argument and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) for not-equal. This can either be a pointer to a function or an object whose class overloads operator().

Return value

An iterator to the first element of the first pair of equal consecutive elements in the range [first,last).
If no such pair is found, the function returns last.

Example

// adjacent_find example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  vector<int> myvector (myints,myints+8);
  vector<int>::iterator it;

  // using default comparison:
  it = adjacent_find (myvector.begin(), myvector.end());

  if (it!=myvector.end())
    cout << "the first consecutive repeated elements are: " << *it << endl;

  //using predicate comparison:
  it = adjacent_find (++it, myvector.end(), myfunction);

  if (it!=myvector.end())
    cout << "the second consecutive repeated elements are: " << *it << endl;
  
  return 0;
}

Output:

the first consecutive repeated elements are: 30
the second consecutive repeated elements are: 10

Complexity

At most, performs as many comparisons or applications of pred as the number of elements in the range [first,last) minus one.

See also

find Find value in range (function template)
find_if Find element in range (function template)
unique Remove consecutive duplicates in range (function template)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us