Source of list10.cpp


  1: //list10.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates how one list can be "
 10:         "assigned to another list\nof the same component type, "
 11:         "using the assignment operator (=), and also\nillustrates "
 12:         "the use of the assign() member function for assigning "
 13:         "values\nto a list.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     cout << "\nFirst we demonstrate use of the assignment operator.";
 17:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 19:     int a1[] = {1, 2, 3, 4, 5};
 20:     list<int> lst1(a1, a1+5);
 21:     cout << "\nFor lst1 we have ...";
 22:     cout << "\nSize = " << lst1.size();
 23:     cout << "\nContents: ";
 24:     list<int>::iterator p = lst1.begin();
 25:     while (p != lst1.end()) cout << *p++ << " ";
 26:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 28:     int a2[] = {1, 2, 3, 4, 5, 6, 7};
 29:     list<int> lst2(a2, a2+7);
 30:     cout << "\nFor lst2 we have ...";
 31:     cout << "\nSize = " << lst2.size();
 32:     cout << "\nContents: ";
 33:     p = lst2.begin();
 34:     while (p != lst2.end()) cout << *p++ << " ";
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 37:     int a3[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 38:     list<int> lst3(a3, a3+10);
 39:     cout << "\nFor lst3 we have ...";
 40:     cout << "\nSize = " << lst3.size();
 41:     cout << "\nContents: ";
 42:     p = lst3.begin();
 43:     while (p != lst3.end()) cout << *p++ << " ";
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 55:     lst2 = lst3;
 56:     cout << "\nNow we assign lst3 to lst2 (lst2 = lst3). "
 57:         "\nThen for lst2 we have ...";
 58:     cout << "\nSize = " << lst2.size();
 59:     cout << "\nContents: ";
 60:     p = lst2.begin();
 61:     while (p != lst2.end()) cout << *p++ << " ";
 62:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 64:     cout << "\nNow we demonstrate use of the assign() member function.";
 65:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 67:     int a4[] = {2, 4, 6, 8};
 68:     list<int> lst4(a4, a4+4);
 69:     cout << "\nFor lst4 we have ...";
 70:     cout << "\nSize = " << lst4.size();
 71:     cout << "\nContents: ";
 72:     p = lst4.begin();
 73:     while (p != lst4.end()) cout << *p++ << " ";
 74:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 76:     lst4.assign(3, 10);
 77:     cout << "\nNow we perform lst4.assign(3, 10). "
 78:         "\nThen for lst4 we have ...";
 79:     cout << "\nSize = " << lst4.size();
 80:     cout << "\nContents: ";
 81:     p = lst4.begin();
 82:     while (p != lst4.end()) cout << *p++ << " ";
 83:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 85:     lst4.assign(6, 12);
 86:     cout << "\nNow we perform lst4.assign(6, 12). "
 87:         "\nThen for lst4 we have ...";
 88:     cout << "\nSize = " << lst4.size();
 89:     cout << "\nContents: ";
 90:     p = lst4.begin();
 91:     while (p != lst4.end()) cout << *p++ << " ";
 92:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 94:     int a5[] = {1, 3, 5, 7, 9};
 95:     list<int> lst5(a5, a5+5);
 96:     cout << "\nFor lst5 we have ...";
 97:     cout << "\nSize = " << lst5.size();
 98:     cout << "\nContents: ";
 99:     p = lst5.begin();
100:     while (p != lst5.end()) cout << *p++ << " ";
101:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

103:     int a6[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
104:     list<int> lst6(a6, a6+10);
105:     cout << "\nFor lst6 we have ...";
106:     cout << "\nSize = " << lst6.size();
107:     cout << "\nContents: ";
108:     p = lst6.begin();
109:     while (p != lst6.end()) cout << *p++ << " ";
110:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

112:     list<int>::iterator p1 = lst6.begin();
113:     list<int>::iterator p2 = lst6.end();
114:     ++p1; ++p1;
115:     --p2; --p2; --p2; --p2;
116:     lst5.assign(p1, p2);
117:     cout << "\nNow we assign the 3rd to the 6th values of lst6 to lst5."
118:         "\nThen for lst5 we have ...";
119:     cout << "\nSize = " << lst5.size();
120:     cout << "\nContents: ";
121:     p = lst5.begin();
122:     while (p != lst5.end()) cout << *p++ << " ";
123:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

125:     p1 = lst6.begin();
126:     p2 = lst6.end();
127:     ++p1;;
128:     --p2; --p2; --p2;
129:     lst5.assign(p1, p2);
130:     cout << "\nNow we assign the 2nd to the 8th values of lst6 to lst5."
131:         "\nThen for lst5 we have ...";
132:     cout << "\nSize = " << lst5.size();
133:     cout << "\nContents: ";
134:     p = lst5.begin();
135:     while (p != lst5.end()) cout << *p++ << " ";
136:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
137: }