equal


Check that two sequences match.

Compare the sequence [ first1 ... last1 ) with a sequence of the same size starting at first2 . Return true if every corresponding pair of elements match. The first version uses operator== to perform the matching, whereas the second version uses the binary function binary_pred .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class InputIterator1, class InputIterator2 >
bool equal
  (
  InputIterator1 first1,
  InputIterator1 last1,
  InputIterator2 first2
  );

template
  <
  class InputIterator1,
  class InputIterator2,
  class BinaryPredicate
  >
bool equal
  (
  InputIterator1 first1,
  InputIterator1 last1,
  InputIterator2 first2,
  BinaryPredicate binary_pred
  );
  

Complexity

Time complexity is linear as n comparisons are performed. Space complexity is constant.

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

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

void
main()
  {
  if ( equal( numbers1, numbers1 + 5, numbers2 ) )
    cout << "numbers1 is equal to numbers2\n";
  else
    cout << "numbers1 is not equal to numbers2\n";

  if ( equal( numbers3, numbers3 + 2, numbers1 ) )
    cout << "numbers3 is equal to numbers1\n";
  else
    cout << "numbers3 is not equal to numbers1\n";

  }

numbers1 is not equal to numbers2
numbers3 is equal to numbers1
Example <ospace/osstd/examples/equal1.cpp>
#include <iostream>
#include <algorithm>
#include <vector>

void
main()
  {
  vector< int > v1( 10 );
  for ( size_t i = 0; i < v1.size(); ++i )
    v1[ i ] = i;
  vector< int > v2( 10 );

  if ( equal( v1.begin(), v1.end(), v2.begin() ))
    cout << "v1 is equal to v2\n";
  else
    cout << "v1 is not equal to v2\n";

  copy( v1.begin(), v1.end(), v2.begin() );

  if ( equal( v1.begin(), v1.end(), v2.begin() ))
    cout << "v1 is equal to v2\n";
  else
    cout << "v1 is not equal to v2\n";

  }

v1 is not equal to v2
v1 is equal to v2
Example <ospace/osstd/examples/equal2.cpp>
#include <iostream>
#include <algorithm>
#include <vector>

bool
values_squared( int a_, int b_ )
  {
  return( a_ * a_ == b_ );
  }

void
main()
  {
  vector< int > v1( 10 );
  vector< int > v2( 10 );
  for ( size_t i = 0; i < v1.size(); ++i )
    {
    v1[ i ] = i;
    v2[ i ] = i * i;
    }

  if ( equal( v1.begin(), v1.end(), v2.begin(), values_squared ) )
    cout << "v2[ i ] == v1[ i ] * v1[ i ]\n";
  else
    cout << "v2[ i ] != v1[ i ] * v1[ i ]\n";

  }

v2[i] == v1[i]*v1[i]

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