Source of rotate_copy1a.cpp


  1: //rotate_copy1a.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_copy() algorithm to\nrotate the order of all, or "
 12:         "just some, of the values in a vector of integers\nwhile "
 13:         "copying the values to an output range in another vector of "
 14:         "integers.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 17:     int a1[] = {1, 2, 3, 4, 5};
 18:     vector<int> v1(a1, a1+5);

 20:     cout << "\nHere are the values in the vector v1:\n";
 21:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 22:         cout << v1.at(i) << " ";
 23:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 25:     int a2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 26:     vector<int> v2(a2, a2+10);

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

 33:     cout << "\nNow we copy all values from v1 to v2, starting at the "
 34:         "beginning of v2,\nand simultaneously rotating the order of the "
 35:         "copied values so that the\nfourth value bcomes the first value.";
 36:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 38:     vector<int>::iterator p;
 39:     p = rotate_copy(v1.begin(), v1.begin()+3, v1.end(), v2.begin());

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

 46:     cout << "\nThe iterator returned by the algorithm is pointing at "
 47:         "the value " << *p << ".";
 48:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 50:     cout << "\nNow we copy the midddle 3 of the values in v1 from v1 to "
 51:         "v2, starting at the\n7th value of v2, and simultaneously rotate "
 52:         "the order of the copied values so\nthat the 3rd of the copied "
 53:         "values becomes the first of the copied group.";
 54:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 56:     p = rotate_copy(v1.begin()+1, v1.begin()+3, v1.end()-1, v2.begin()+6);

 58:     cout << "\nHere are the revised values in v2:\n";
 59:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 60:         cout << v2.at(i) << " ";
 61:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 63:     cout << "\nThe iterator returned by the algorithm is pointing at "
 64:         "the value " << *p << ".";
 65:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 66: }