1: //list01.cpp
3: #include <iostream>
4: #include <string>
5: #include <list>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates all of the STL list container "
11: "class constructors, as\nwell as the default list iterator and the "
12: "member functions begin() and end().\nIt also shows how ordinary "
13: "pointers can be used, in certain situations, in the\nsame way that "
14: "STL iterators are used.";
15: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
17: //Create an empty list
18: list<int> lst1;
19: cout << "\nContents of lst1: ";
20: list<int>::iterator p_i = lst1.begin();
21: while (p_i != lst1.end()) cout << *p_i++ << " ";
22: cout << endl;
23: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
25: //Create a list filled with the default component-type value
26: list<int> lst2(5);
27: cout << "\nContents of lst2: ";
28: p_i = lst2.begin();
29: while (p_i != lst2.end()) cout << *p_i++ << " ";
30: cout << endl;
31: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
33: //Create a list filled with a specific component-type value
34: list<double> lst3(4, 3.14);
35: cout << "\nContents of lst3: ";
36: list<double>::iterator p_d = lst3.begin();
37: while (p_d != lst3.end()) cout << *p_d++ << " ";
38: cout << endl;
39: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
41: //Create a list using pointers to an integer array
42: int a[] = {2, 4, 6, 8, 10, 12};
43: list<int> lst4(a, a + sizeof(a)/sizeof(int));
44: cout << "\nContents of lst4: ";
45: p_i = lst4.begin();
46: while (p_i != lst4.end()) cout << *p_i++ << " ";
47: cout << endl;
48: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
50: //Create a list using pointers to a C-string
51: char s[] = "Hello";
52: list<char> lst5(s, s + strlen(s));
53: cout << "\nContents of lst5: ";
54: list<char>::iterator p_c = lst5.begin();
55: while (p_c != lst5.end()) cout << *p_c++ << " ";
56: cout << endl;
57: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
59: //Create a new list as a copy of an existing list
60: list<double> lst6(lst3); //or list<double> lst6 = d3;
61: cout << "\nContents of lst6 same as those of lst3: ";
62: p_d = lst6.begin();
63: while (p_d != lst6.end()) cout << *p_d++ << " ";
64: cout << endl;
65: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
66: }