Source of vector_a.cpp


  1: //vector_a.cpp

  3: #include <iostream>
  4: #include <algorithm>
  5: #include <iterator> //Redundant in VS2005 but required for g++ 3.3.3.
  6: #include <vector>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the istream_iterator "
 12:         "and ostream_iterator\nin conjunction with the copy algorithm "
 13:         "to provide \"vector I/O\".";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a[] = {1, 2, 3, 4, 5};
 17:     vector<int> v(a, a+5);
 18:     cout << "\nInitial contents of v, displayed using the copy "
 19:         "algorithm to copy values from\nv to the standard output "
 20:         "with the help of an ostream_iterator:\n";
 21:     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     cout << "\nNow we copy some values from standard input into a vector "
 25:         "\nusing the copy algorithm and an istream_iterator.\nEnter five "
 26:         "integer values, followed by your end-of-file character:\n";
 27:     copy(istream_iterator<int>(cin), istream_iterator<int>(), v.begin());
 28:     cin.ignore(80, '\n');
 29:     cin.clear();

 31:     cout << "\nFinal contents of v: ";
 32:     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 34: }