Source of list06.cpp


  1: //list06.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates typical uses of the default "
 11:         "list class iterator\n(which is a random access iterator), "
 12:         "and also shows the use of operators\n-> and  == with list "
 13:         "class iterators.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a[] = {1, 2, 4, 8, 16, 32, 64};
 17:     list<int> lst(a, a+7);

 19:     list<int>::iterator p = lst.begin();
 20:     cout << "\nHere are the values in our list:\n";
 21:     while (p != lst.end()) cout << *p++ << " ";  cout << endl;
 22:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 24:     list<int>::iterator p1 = lst.begin(); //point at first component
 25:     list<int>::iterator p2 = lst.end();   //point at one-past-the-last
 26:     cout << "\np1 is set to point at the first component and "
 27:         "p2 to one-past the last.\n";
 28:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 30:     if (p1 == p2)
 31:         cout << "\np1 == p2, so *p1 == *p2 == " << *p1;
 32:     else
 33:         cout << "\np1 and p2 point to different list elements.";
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 36:     struct Point{int x; int y;};
 37:     Point location1 = {1, 2}, location2 = {4, 5};
 38:     list<Point> lst2;
 39:     lst2.push_back(location1);
 40:     lst2.push_back(location2);
 41:     list<Point>::iterator pp = lst2.begin();
 42:     cout << "\nLocations (accessed with the -> operator): ";
 43:     cout << "(" << pp->x << ", " << pp->y << ") ";
 44:     ++pp;
 45:     cout << "(" << pp->x << ", " << pp->y << ") ";
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 47: }