Source of unique1a.cpp


  1: //unique1a.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() "
 12:         "algorithm\n(default version) to remove adjacent duplicate "
 13:         "copies of integer\nvalues from a vector of integers.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a1[] = {1, 2, 2, 3, 3, 6, 5, 2, 6, 9, 12, 2, 2, 2, 9, 10, 11, 12};
 17:     vector<int> v1(a1, a1+18);
 18:     cout << "\nHere are the values in the first vector:\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:     cout << "\nNow we remove all adjacent duplicate copies of the "
 24:         "\nvalues 2 and 3 and redisplay the values to confirm:\n";
 25:     vector<int>::iterator new_end = unique(v1.begin(), v1.end());
 26:     vector<int>::iterator p = v1.begin();
 27:     while (p != new_end) cout << *p++ << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     int a2[] = {1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 10, 10, 10};
 31:     vector<int> v2(a2, a2+18);
 32:     cout << "\nHere are the values in the second vector:\n";
 33:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 34:         cout << v2.at(i) << " ";;
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 37:     cout << "\nNow we remove all adjacent duplicate copies of any "
 38:         "\nof the values and redisplay the values to confirm:\n";
 39:     new_end = unique(v2.begin(), v2.end());
 40:     p = v2.begin();
 41:     while (p != new_end) cout << *p++ << " ";
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 43: }