Source of unique_copy1a.cpp


  1: //unique_copy1a.cpp

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


  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL unique_copy() "
 12:         "algorithm (default\nversion) to remove adjacent duplicate copies "
 13:         "of integer values from a vector\nof integers, and simultaneously "
 14:         "copy the remaining values to another vector.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 17:     int a11[] = {1, 2, 2, 3, 3, 6, 5, 2, 6, 9, 12, 2, 2, 2, 9, 10, 11, 12};
 18:     vector<int> v11(a11, a11+18);
 19:     cout << "\nHere are the values in the source vector:\n";
 20:     for (vector<int>::size_type i=0; i<v11.size(); i++)
 21:         cout << v11.at(i) << " ";;
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     int a12[] = {21, 22, 23, 24, 25, 26, 27, 28, 29,
 25:                  30, 31, 32, 33, 34, 35, 36, 37, 38};
 26:     vector<int> v12(a12, a12+18);
 27:     cout << "\nHere are the values in the destination vector:\n";
 28:     for (vector<int>::size_type i=0; i<v12.size(); i++)
 29:         cout << v12.at(i) << " ";;
 30:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 32:     cout << "\nNow we remove all adjacent duplicate copies of the "
 33:         " values 2 and 3 from\nthe source vector and copy the remaining "
 34:         "values to the destination vector,\nstarting at the third value "
 35:         "in that destination vector.";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 37:     vector<int>::iterator output_end =
 38:         unique_copy(v11.begin(), v11.end(), v12.begin()+2);

 40:     cout << "\nHere are the (unchanged) source vector values:\n";
 41:     vector<int>::iterator p = v11.begin();
 42:     while (p != v11.end()) cout << *p++ << " ";
 43:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 45:     cout << "\nAnd here are the destination vector values:\n";
 46:     p = v12.begin();
 47:     while (p != v12.end()) cout << *p++ << " ";
 48:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 50:     cout << "\nThe iterator returned by the call to unique_copy() is "
 51:         "pointing to the value " << *output_end << ".";
 52:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 53: }