Check for mismatch between two vectors : mismatch « STL Algorithms Non modifying sequence operations « C++






Check for mismatch between two vectors

 
 

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

   std::pair< std::vector< int >::iterator, std::vector< int >::iterator > loca
tion;

   location = std::mismatch( v1.begin(), v1.end(), v3.begin() );
   cout << "\nThere is a mismatch between v1 and v3 at location "
      << ( location.first - v1.begin() ) << "\nwhere v1 contains "
      << *location.first << " and v3 contains " << *location.second;

   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
There is a mismatch between v1 and v3 at location 4
where v1 contains 5 and v3 contains 9
 */
        
  








Related examples in the same category

1.Generic mismatch algorithm: Find the corresponding positions in a deque and list at which unequal elements first occur
2.Use mismatch function to find the first match
3.Use mismatch function with custom function