Source of mismatch1a.cpp


  1: //mismatch1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: #include <utility>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL mismatch() "
 12:         "algorithm (default\nversion) to find pairs of mismatched "
 13:         "values in two ranges of integers from\ntwo vectors of integers "
 14:         "when simple equality is used to determine if two\ninteger "
 15:         "values match.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 18:     int a1[] = {4, 3, 7, 5, 6};
 19:     vector<int> v1(a1, a1+5);
 20:     cout << "\nHere are the contents of v1:\n";
 21:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 22:         cout << v1.at(i) << " ";
 23:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 25:     int a2[] = {1, 2, 4, 3, 1, 5, 6, 8, 9, 10};
 26:     vector<int> v2(a2, a2+10);
 27:     cout << "\nHere are the contents of v2:\n";
 28:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 29:         cout << v2.at(i) << " ";
 30:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 32:     pair<vector<int>::iterator, vector<int>::iterator> mismatches;

 34:     cout << "\nNow we compare the sequence in v1 with the sequence "
 35:         "\nstarting at the third value in v2.";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 38:     mismatches = mismatch(v1.begin(), v1.end(), v2.begin()+2);
 39:     if (mismatches.first == v1.end())
 40:         cout << "\nThe two sequences match fully.";
 41:     else
 42:         cout << "\nThe first pair of mismatched values are\n"
 43:             << *mismatches.first << " from the first range, and\n"
 44:             << *mismatches.second << " from the second range.";
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 47:     int a3[] = {4, 3, 7, 5, 6, 2, 10, 9};
 48:     vector<int> v3(a3, a3+8);
 49:     cout << "\nHere are the contents of v3:\n";
 50:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 51:         cout << v3.at(i) << " ";
 52:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 54:     cout << "\nFinally, we compare the sequence in v1 with the "
 55:         "sequence in v3.";
 56:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 58:     mismatches = mismatch(v1.begin(), v1.end(), v3.begin());
 59:     if (mismatches.first == v1.end())
 60:     {
 61:         cout << "\nThe two sequences match fully.";
 62:         cout << "\nThe second iterator of the pair returned points at\n"
 63:             << *mismatches.second << " in the second range (in v2).";
 64:     }
 65:     else
 66:         cout << "\nThe first pair of mismatched values are\n"
 67:             << *mismatches.first << " from the first range, and\n"
 68:             << *mismatches.second << " from the second range.";
 69:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 70: }