Comparing sequences of values for equality using algorithms mismatch - C++ STL Algorithm

C++ examples for STL Algorithm:mismatch

Description

Comparing sequences of values for equality using algorithms mismatch

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <vector> // vector class-template definition 
#include <iterator> // ostream_iterator 
using namespace std;

int main()/* ww  w . ja  va 2s  .c om*/
{
  const int SIZE = 10;
  int a1[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int a2[SIZE] = { 1, 2, 3, 4, 1000, 6, 7, 8, 9, 10 };
  vector< int > v1(a1, a1 + SIZE); // copy of a1 
  vector< int > v2(a1, a1 + SIZE); // copy of a1 
  vector< int > v3(a2, a2 + SIZE); // copy of a2 
  ostream_iterator< int > output(cout, " ");

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

  // location represents pair of vector iterators 
  pair< vector< int >::iterator, vector< int >::iterator > location;

  // check for mismatch between v1 and v3 
  location = 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
    << "\n\n";

  char c1[SIZE] = "HELLO";
  char c2[SIZE] = "BYE BYE";

  // perform lexicographical comparison of c1 and c2 
  int result = lexicographical_compare(c1, c1 + SIZE, c2, c2 + SIZE);
  cout << c1 << (result ? " is less than " :
    " is greater than or equal to ") << c2 << endl;
}

Result


Related Tutorials