Use std::equal to compare vectors v1 and v2 for equality : equal « STL Algorithms Non modifying sequence operations « C++






Use std::equal to compare vectors v1 and v2 for equality

 
 

#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
   int a1[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   int a2[ 10 ] = { 1, 2, 3, 4, 9, 6, 7, 8, 9, 10 };
   std::vector< int > v1( a1, a1 + 10 );
   std::vector< int > v2( a1, a1 + 10 );
   std::vector< int > v3( a2, a2 + 10 );
   std::ostream_iterator< int > output( cout, " " );

   cout << "Vector v1 contains: ";
   std::copy( v1.begin(), v1.end(), output );
   cout << "\nVector v2 contains: ";
   std::copy( v2.begin(), v2.end(), output );
   cout << "\nVector v3 contains: ";
   std::copy( v3.begin(), v3.end(), output );

   // compare vectors v1 and v2 for equality
   bool result = std::equal( v1.begin(), v1.end(), v2.begin() );
   cout << "\n\nVector v1 " << ( result ? "is" : "is not" )
      << " equal to vector v2.\n";

   return 0;
}

/* 
Vector v1 contains: 1 2 3 4 5 6 7 8 9 10
Vector v2 contains: 1 2 3 4 5 6 7 8 9 10
Vector v3 contains: 1 2 3 4 9 6 7 8 9 10

Vector v1 is equal to vector v2.

 */        
  








Related examples in the same category

1.equal algorithm: a deque and the first 2 elements of a list are not equal in all corresponding positions
2.equal algorithm: a list and the first 3 elements of a vector are equal in all corresponding positions