Source of copy_backward1a.cpp


  1: //copy_backward1a.cpp

  3: #include <iostream>
  4: #include <iomanip>
  5: #include <vector>
  6: #include <algorithm>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL "
 12:         "copy_backward() algorithm\nto copy integer values from "
 13:         "one vector to another.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');


 17:     int evens[] = {0, 2, 4, 6, 8, 10};
 18:     vector<int> v1(evens, evens+6);
 19:     cout << "\nHere are the contents of v1:\n";
 20:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 21:         cout << v1.at(i) << " ";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     int odds[] = {1, 3, 5, 7, 9};
 25:     vector<int> v2(odds, odds+5);
 26:     cout << "\nHere are the contents of v2:\n";
 27:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 28:         cout << v2.at(i) << " ";
 29:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 31:     cout << "\nNow we use the copy_backward algorithm to copy the "
 32:         "middle 3 of the 5 values of\nthe second vector to the first "
 33:         "vector. The 5th element of the first vector is\nthe end of "
 34:         "the destination range to which we copy.";
 35:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 37:     vector<int>::iterator p;
 38:     //We may ignore the (iterator) return value of the algorithm,
 39:     //but here we save it, to see what it's pointing at:
 40:     p = copy_backward(v2.begin()+1, v2.begin()+4, v1.begin()+4);

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

 47:     cout << "\nThe value pointed to by the iterator returned "
 48:         "by the copy_backward()\nalgorithm is " << *p
 49:         << " (the last value actually copied).";
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 51: }