adjacent_find


Locate a consecutive sequence in a range.

Return an input iterator positioned at the first pair of matching consecutive elements. If no match is found, return last . The first version performs matching by using operator== , whereas the second version uses the binary function binary_pred .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class InputIterator >
InputIterator adjacent_find
  (
  InputIterator first,
  InputIterator last
  );

template< class InputIterator, class BinaryPredicate >
InputIterator adjacent_find
  (
  InputIterator first,
  InputIterator last,
  BinaryPredicate binary_pred
  );
  

Complexity

Time complexity is linear, as a maximum of ( last - first ) comparisons are performed. Space complexity is constant.

Example <ospace/osstd/examples/adjfind0.cpp>
#include <iostream>
#include <algorithm>

int numbers1[ 5 ] = { 1, 2, 4, 8, 16 };
int numbers2[ 5 ] = { 5, 3, 2, 1, 1 };

void
main()
  {
  int* location = adjacent_find( numbers1, numbers1 + 5 );

  if ( location != numbers1 + 5 )
    cout
      << "Found adjacent pair of: "
      << *location
      << " at offset "
      << ( location - numbers1 )
      << "\n";
  else
    cout << "No adjacent pairs\n";

  location = adjacent_find( numbers2, numbers2 + 5 );

  if ( location != numbers2 + 5 )
    cout
      << "Found adjacent pair of: "
      << *location
      << " at offset "
      << ( location - numbers2 )
      << "\n";
  else
    cout << "No adjacent pairs\n";
  }

No adjacent pairs
Found adjacent pair of: 1 at offset 3
Example <ospace/osstd/examples/adjfind1.cpp>
#include <iostream>
#include <algorithm>
#include <vector>

void
main()
  {
  vector< int > v( 10 );
  for ( size_t i = 0; i < v.size(); ++i )
    v[ i ] = i;
  vector< int >::iterator location;

  location = adjacent_find( v.begin(), v.end() );

  if ( location != v.end() )
    cout << "Found adjacent pair of: " << *location << "\n";
  else
    cout << "No adjacent pairs\n";
  v[ 6 ] = 7;

  location = adjacent_find( v.begin(), v.end() );

  if ( location != v.end() )
    cout << "Found adjacent pair of: " << *location << "\n";
  else
    cout << "No adjacent pairs\n";
  }

No adjacent pairs
Found adjacent pair of: 7
Example <ospace/osstd/examples/adjfind2.cpp>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>

int
equal_length( const char* v1, const char* v2 )
  {
  return ::strlen( v1 ) == ::strlen( v2 );
  }

char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" };

void
main()
  {
  const int name_count = sizeof( names ) / sizeof( names[ 0 ] );
  vector< char* > v( name_count );
  for ( int i = 0; i < name_count; ++i )
    v[ i ] = names[ i ];
  vector< char* >::iterator location;

  location = adjacent_find( v.begin(), v.end(), equal_length );

  if ( location != v.end() )
    cout
      << "Found two adjacent strings of equal length: "
      << *location
      << " -and- "
      << *( location + 1 )
      << "\n";
  else
    cout << "Didn't find two adjacent strings of equal length.";

  }

Found two adjacent strings of equal length: Jack -and- Mike

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.