Source of list07.cpp


  1: //list07.cpp

  3: #include <iostream>
  4: #include <list>
  5: using namespace std;

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates reverse iterators of the "
 10:         "list class,\nas well as member functions rbegin() and "
 11:         "rend().";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     cout << "\nHere are the contents of a list of size 12:\n";
 15:     int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24};
 16:     list<int> lst(a, a+12);
 17:     list<int>::iterator p = lst.begin();
 18:     while (p != lst.end()) cout << *p++ << " ";
 19:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 21:     cout << "\nNow using a reverse iterator to display\n"
 22:         "the list components in reverse order:\n";
 23:     list<int>::reverse_iterator r_p = lst.rbegin();
 24:     while (r_p != lst.rend()) cout << *r_p++ << " ";
 25:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 27:     cout << "\nNow using a reverse iterator to display\n"
 28:         "the list components in forward order:\n";
 29:     r_p = lst.rend();
 30:     while (r_p != lst.rbegin()) cout << *--r_p << " ";
 31:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 33:     cout << "\nNow constructing a new list containing the "
 34:         "values from the first list\nin reverse order, and "
 35:         "then displaying the values from this new list.\n";
 36:     list<int> lst1(lst.rbegin(), lst.rend());
 37:     p = lst1.begin();
 38:     while (p != lst1.end()) cout << *p++ << " ";
 39:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 40: }