Source of list_a.cpp


  1: //list_a.cpp

  3: #include <iostream>
  4: #include <algorithm>
  5: #include <iterator> //Redundant in VS2005 but required for g++ 3.3.3.
  6: #include <list>
  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 \"list I/O\".";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a[] = {1, 2, 3, 4, 5};
 17:     list<int> lst(a, a+5);
 18:     cout << "\nInitial contents of lst, displayed using the copy "
 19:         "algorithm to copy values from\nlst to the standard output "
 20:         "with the help of an ostream_iterator:\n";
 21:     copy(lst.begin(), lst.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 list "
 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>(), lst.begin());
 28:     cin.ignore(80, '\n');
 29:     cin.clear();

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