Source of reverse1a.cpp


  1: //reverse1a.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 "
 11:         "reverse() algorithm to reverse\nthe order of all, or "
 12:         "just some, of the values in a vector of integers.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 16:     vector<int> v(a, a+10);

 18:     cout << "\nHere are the contents of the vector:\n";
 19:     for (vector<int>::size_type i=0; i<v.size(); i++)
 20:         cout << v.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 23:     cout << "\nNow we reverse the order of all values in the vector.";
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 26:     reverse(v.begin(), v.end());

 28:     cout << "\nHere are the revised contents of the vector:\n";
 29:     for (vector<int>::size_type i=0; i<v.size(); i++)
 30:         cout << v.at(i) << " ";
 31:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 33:     cout << "\nNow we reverse the order of all values in the vector, "
 34:         "except the end values.";
 35:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 37:     reverse(v.begin()+1, v.end()-1);

 39:     cout << "\nHere are the revised contents of the vector:\n";
 40:     for (vector<int>::size_type i=0; i<v.size(); i++)
 41:         cout << v.at(i) << " ";
 42:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 43: }