Source of reverse_copy1a.cpp


  1: //reverse_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:         "reverse_copy() algorithm to\nreverse 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 reverse the order of the "
 35:         "copied values.";
 36:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 38:     vector<int>::iterator p;
 39:     p = reverse_copy(v1.begin(), 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 reverse "
 52:         "the order of the copied values.";
 53:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 55:     p = reverse_copy(v1.begin()+1, v1.end()-1, v2.begin()+6);

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

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