Source of list12.cpp


  1: //list12.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of member functions "
 11:         "clear(),\nerase(), pop_back() and pop_front() for list "
 12:         "objects.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 16:     list<int> lst(a, a+10);
 17:     cout << "\nFor our starting list list we have ...";
 18:     cout << "\nSize = " << lst.size();
 19:     cout << "\nContents: ";
 20:     list<int>::iterator p = lst.begin();
 21:     while (p != lst.end()) cout << *p++ << " ";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     cout << "\nFirst, we delete the third component of the list, "
 25:         "\nand then display the remaining values in the list.";
 26:     list<int>::iterator pErase = lst.begin();
 27:     for (int i=1; i<=2; i++) ++pErase;
 28:     list<int>::iterator pReturn = lst.erase(pErase);
 29:     cout << "\nSize = " << lst.size();
 30:     cout << "\nContents: ";
 31:     p = lst.begin();
 32:     while (p != lst.end()) cout << *p++ << " ";
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 34:     cout << "\nThe iterator returned by the above call to erase() "
 35:         "points to " << *pReturn << ".\n";
 36:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 38:     list<int>::iterator eraseBegin = lst.begin();
 39:     list<int>::iterator eraseEnd = lst.begin();
 40:     for (int i=1; i<=2; i++) ++eraseBegin;
 41:     for (int i=1; i<=7; i++) ++eraseEnd;
 42:     cout << "\nNext, we delete the 3rd through 7th components of "
 43:         "the list\nand then display the remaining values in the list.";
 44:     pReturn = lst.erase(eraseBegin, eraseEnd);
 45:     cout << "\nSize = " << lst.size();
 46:     cout << "\nContents: ";
 47:     p = lst.begin();
 48:     while (p != lst.end()) cout << *p++ << " ";
 49:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 50:     cout << "\nThe iterator returned by the above call to erase() "
 51:         "points to " << *eraseEnd << ".\n";
 52:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 54:     cout << "\nNow we remove a value from the end of lst (using "
 55:         "lst.pop_back()),\nand display again to confirm.";
 56:     lst.pop_back();
 57:     cout << "\nSize = " << lst.size();
 58:     cout << "\nContents: ";
 59:     p = lst.begin();
 60:     while (p != lst.end()) cout << *p++ << " ";
 61:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 63:     cout << "\nThen we remove a value from the front of lst (using "
 64:         "lst.pop_front()),\nand display again to confirm.";
 65:     lst.pop_front();
 66:     cout << "\nSize = " << lst.size();
 67:     cout << "\nContents: ";
 68:     p = lst.begin();
 69:     while (p != lst.end()) cout << *p++ << " ";
 70:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 72:     cout << "\nFinally, we clear the list and display once again "
 73:         "to confirm:";
 74:     lst.clear();
 75:     cout << "\nSize = " << lst.size();
 76:     cout << "\nContents: ";
 77:     p = lst.begin();
 78:     while (p != lst.end()) cout << *p++ << " ";
 79:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 80: }