Source of copy1b.cpp


  1: //copy1b.cpp

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

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL copy() "
 12:         "algorithm to copy integers\nfrom a vector to the standard "
 13:         "output, using an output stream iterator.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
 17:     vector<int> v(a, a+10);
 18:     cout << "\nHere are the contents of v, displayed in the "
 19:         "\"usual\" way:\n";
 20:     for (vector<int>::size_type i=0; i<v.size(); i++)
 21:         cout << v.at(i) << " ";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     cout << "\nAnd here they are again, displayed this time using "
 25:         "\nthe copy algorithm and an output stream iterator:\n";
 26:     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 28: }