Source of rotate1a.cpp


  1: //rotate1a.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:         "rotate() algorithm to rotate\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 rotate the order of all values in the vector "
 24:         "\nin so that the fourth value becomes the first value.";
 25:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 27:     rotate(v.begin(), v.begin()+3, v.end());

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

 34:     cout << "\nNext we rotate the order of the 2nd to the 6th values "
 35:         "in the vector so that\nthe third value in that group of values "
 36:         "becomes the first value of the group.";
 37:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 39:     rotate(v.begin()+1, v.begin()+3, v.begin()+6);

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