1: //list11.cpp
3: #include <iostream>
4: #include <list>
5: using namespace std;
7: int main()
8: {
9: cout << "\nThis program illustrates the insert() member function.";
10: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
12: int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
13: list<int> lst(a, a+10);
14: cout << "\nFor our starting list we have ...";
15: cout << "\nSize = " << lst.size();
16: cout << "\nContents: ";
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: list<int>::iterator pInsert = lst.begin();
22: for (int i=1; i<=3; i++) ++pInsert;
23: list<int>::iterator pReturn = lst.insert(pInsert, 17);
24: cout << "\nNow we insert 17 at position 4 in the list."
25: "\nThen for the list we have ...";
26: cout << "\nSize = " << lst.size();
27: cout << "\nContents: ";
28: p = lst.begin();
29: while (p != lst.end()) cout << *p++ << " ";
30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
32: cout << "\nAnd the value pointed to by the iterator\nreturned "
33: "by this call to insert() is also " << *pReturn << ".";
34: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
36: pInsert = lst.begin();
37: for (int i=1; i<=8; i++) ++pInsert;
38: lst.insert(pInsert, 5, 33);
39: cout << "\nNext we insert 5 copies of 33 at the 9th position "
40: "in the list.\nThen for our list we have ...";
41: cout << "\nSize = " << lst.size();
42: cout << "\nContents: ";
43: p = lst.begin();
44: while (p != lst.end()) cout << *p++ << " ";
45: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
47: pInsert = lst.begin();
48: for (int i=1; i<=10; i++) ++pInsert;
49: lst.insert(pInsert, a+1, a+4);
50: cout << "\nNext we insert the 2nd to 4th values from the array "
51: "\nused to create our original list at position 11 in our list."
52: "\nThen for lst we have ...";
53: cout << "\nSize = " << lst.size();
54: cout << "\nContents: ";
55: p = lst.begin();
56: while (p != lst.end()) cout << *p++ << " ";
57: cout << "\nPress enter to continue ... "; cin.ignore(80, '\n');
59: pInsert = lst.begin();
60: for (int i=1; i<=7; i++) ++pInsert;
61: list<int>::iterator p1 = lst.begin();
62: list<int>::iterator p2 = lst.begin();
63: for (int i=1; i<=2; i++) ++p1;
64: for (int i=1; i<=5; i++) ++p2;
65: lst.insert(pInsert, p1, p2);
66: cout << "\nFinally we insert 3rd to the 5th values from the list "
67: "itself,\ninto the list at position 8."
68: "\nThen for lst we have ...";
69: cout << "\nSize = " << lst.size();
70: cout << "\nContents: ";
71: p = lst.begin();
72: while (p != lst.end()) cout << *p++ << " ";
73: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
74: }