Source of list13.cpp


  1: //list13.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the resize() member "
 11:         "function for list objects.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     list<int> lst1;
 15:     cout << "\nTo begin we have, for lst1:";
 16:     cout << "\nSize = " << lst1.size();
 17:     cout << "\nContents: ";
 18:     list<int>::iterator p = lst1.begin();
 19:     while (p != lst1.end()) cout << *p++ << " ";
 20:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 22:     lst1.resize(4);
 23:     cout << "\nAfter lst1.resize(4) we have, for lst1:";
 24:     cout << "\nSize = " << lst1.size();
 25:     cout << "\nContents: ";
 26:     p = lst1.begin();
 27:     while (p != lst1.end()) cout << *p++ << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     list<int> lst2(10, 3);
 31:     cout << "\nTo begin we have, for lst2:";
 32:     cout << "\nSize = " << lst2.size();
 33:     cout << "\nContents: ";
 34:     p = lst2.begin();
 35:     while (p != lst2.end()) cout << *p++ << " ";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 38:     lst2.resize(8);
 39:     cout << "\nAfter lst2.resize(8) we have, for lst2:";
 40:     cout << "\nSize = " << lst2.size();
 41:     cout << "\nContents: ";
 42:     p = lst2.begin();
 43:     while (p != lst2.end()) cout << *p++ << " ";
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 46:     lst2.resize(12);
 47:     cout << "\nAfter lst2.resize(12) we have, for lst2:";
 48:     cout << "\nSize = " << lst2.size();
 49:     cout << "\nContents: ";
 50:     p = lst2.begin();
 51:     while (p != lst2.end()) cout << *p++ << " ";
 52:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 54:     lst2.resize(6, -1);
 55:     cout << "\nAfter lst2.resize(6, -1) we have, for lst2:";
 56:     cout << "\nSize = " << lst2.size();
 57:     cout << "\nContents: ";
 58:     p = lst2.begin();
 59:     while (p != lst2.end()) cout << *p++ << " ";
 60:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 62:     lst2.resize(10, -1);
 63:     cout << "\nAfter lst2.resize(10, -1) we have, for lst2:";
 64:     cout << "\nSize = " << lst2.size();
 65:     cout << "\nContents: ";
 66:     p = lst2.begin();
 67:     while (p != lst2.end()) cout << *p++ << " ";
 68:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 70:     lst2.resize(20, -2);
 71:     cout << "\nAfter lst2.resize(20, -2) we have, for lst2:";
 72:     cout << "\nSize = " << lst2.size();
 73:     cout << "\nContents: ";
 74:     p = lst2.begin();
 75:     while (p != lst2.end()) cout << *p++ << " ";
 76:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 77: }