Source of equal1a.cpp


  1: //equal1a.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL equal() "
 11:         "algorithm (default version)\nto test whether the integers in a "
 12:         "range of integers in one vector are the same\nas the integers "
 13:         "in another range in a different vector.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

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

 30:     cout << "\nFirst we compare the sequence in v1 and the sequence "
 31:         "starting\nat the beginning of v2, and ask whether they match.";
 32:     if (equal(v1.begin(), v1.end(), v2.begin()))
 33:         cout << "\nYes, they match.";
 34:     else
 35:         cout << "\nNo, they don't match.";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 38:     cout << "\nThen we compare the sequence in v1 with the sequence "
 39:         "starting\nat the third value of v2, and ask whether they match.";
 40:     if (equal(v1.begin(), v1.end(), v2.begin()+2))
 41:         cout << "\nYes, they match.";
 42:     else
 43:         cout << "\nNo, they don't match.";
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 45: }