1: //list15.cpp
3: #include <iostream>
4: #include <string>
5: #include <list>
6: using namespace std;
8: bool isDivisibleByThree
9: (
10: int n //in
11: )
12: /**<
13: Determine if an integer is divisible by 3.
14: @return true if the input integer is divisible by 3, and otherwise false.
15: @param n An integer.
16: @pre n contains an integer.
17: @post No side effects.
18: */
19: {
20: return (n%3 == 0);
21: }
23: int main()
24: {
25: cout << "\nThis program illustrates the remove() and remove_if() "
26: "\nmember functions for list objects.";
27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
29: int a[] = {1, 2, 2, 3, 4, 5, 2, 6, 7, 8, 2, 2, 2, 9, 10, 11, 12};
30: list<int> lst(a, a+17);
31: cout << "\nFor the starting list we have:";
32: cout << "\nSize = " << lst.size();
33: cout << "\nContents: ";
34: list<int>::iterator p = lst.begin();
35: while (p!= lst.end()) cout << *p++ << " ";
36: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
38: cout << "\nNow we remove all copies of the value 2 and "
39: "redisplay the list to confirm:";
40: lst.remove(2);
41: p = lst.begin();
42: cout << "\nSize = " << lst.size();
43: cout << "\nContents: ";
44: while (p!= lst.end()) cout << *p++ << " ";
45: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
47: cout << "\nFinally we remove all values divisible by 3 and "
48: "redisplay the list to confirm:";
49: lst.remove_if(isDivisibleByThree);
50: p = lst.begin();
51: cout << "\nSize = " << lst.size();
52: cout << "\nContents: ";
53: while (p!= lst.end()) cout << *p++ << " ";
54: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
55: }