Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg4.cpp
Go to the documentation of this file.
1
30#include <list>
31#include <set>
32#include <algorithm>
33#include <iostream>
34#include <iterator>
35
36#include <examples.h>
37
38typedef std::ostream_iterator<int, char, std::char_traits<char> >
40
41
42bool isEven (int n) {
43 return 0 == (n % 2);
44}
45
46
47// Illustrate the use of the remove algorithm.
49{
50 std::cout << "Remove Algorithm examples" << std::endl;
51
52 // Create a list of numbers.
53 int data[] = { 1, 2, 4, 3, 1, 4, 2 };
54 std::list<int, std::allocator<int> > aList;
55
56 std::copy (data, data+7, std::inserter (aList, aList.begin ()));
57 std::cout << "Original list: ";
58 std::copy (aList.begin (), aList.end (),
59 ostrm_iter_type (std::cout, " "));
60
61 std::cout << std::endl;
62
63 // Remove 2's, copy into a new list.
64 std::list<int, std::allocator<int> > newList;
65
66 std::remove_copy (aList.begin (), aList.end (),
67 std::back_inserter (newList), 2);
68 std::cout << "After removing 2's: ";
69 std::copy (newList.begin (), newList.end (),
70 ostrm_iter_type (std::cout, " "));
71
72 std::cout << std::endl;
73
74 // Remove 2's in place.
75 std::list<int, std::allocator<int> >::iterator
76 where = std::remove (aList.begin (), aList.end (), 2);
77
78 std::cout << "List after removal, before erase: ";
79 std::copy (aList.begin (), aList.end (),
80 ostrm_iter_type (std::cout, " "));
81
82 std::cout << std::endl;
83
84 aList.erase (where, aList.end ());
85 std::cout << "List after erase: ";
86 std::copy (aList.begin (), aList.end (),
87 ostrm_iter_type (std::cout, " "));
88
89 std::cout << std::endl;
90
91 // Remove all even values.
92 where = std::remove_if (aList.begin (), aList.end (), isEven);
93 aList.erase (where, aList.end ());
94
95 std::cout << "List after removing even values: ";
96 std::copy (aList.begin (), aList.end (),
97 ostrm_iter_type (std::cout, " "));
98
99 std::cout << std::endl;
100}
101
102
103// Illustrate use of the unqiue algorithm.
105{
106 // First make a list of values.
107 int data[] = { 1, 3, 3, 2, 2, 4 };
108 std::list<int, std::allocator<int> > aList;
109
110 std::copy (data, data+6, std::inserter (aList, aList.begin ()));
111 std::cout << "Origianal List: ";
112 std::copy (aList.begin (), aList.end (),
113 ostrm_iter_type (std::cout, " "));
114
115
116 std::cout << std::endl;
117
118 // Copy unique elements into a set.
119 std::set<int, std::less<int>, std::allocator<int> > aSet;
120
121 std::unique_copy (aList.begin (), aList.end (),
122 std::inserter (aSet, aSet.begin ()));
123
124 std::cout << "Set after unique_copy: ";
125 std::copy (aSet.begin (), aSet.end (), ostrm_iter_type (std::cout, " "));
126
127 std::cout << std::endl;
128
129 // Copy unique elements in place.
130 std::list<int, std::allocator<int> >::iterator
131 where = std::unique (aList.begin (), aList.end ());
132
133 std::cout << "List after calling unique: ";
134 std::copy (aList.begin (), aList.end (),
135 ostrm_iter_type (std::cout, " "));
136
137 std::cout << std::endl;
138
139 // Remove trailing values.
140 aList.erase (where, aList.end ());
141
142 std::cout << "List after erase: ";
143 std::copy (aList.begin (), aList.end (),
144 ostrm_iter_type (std::cout, " "));
145
146 std::cout << std::endl;
147
148}
149
150int main ()
151{
152 std::cout << "STL generic algorithms -- Removal Algorithms"
153 << std::endl;
154
157
158 std::cout << "End of removal algorithms sample program"
159 << std::endl;
160
161 return 0;
162}
#define n
Definition 17.names.cpp:130
const char *const data[]
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg3.cpp:45
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg4.cpp:39
void remove_example()
Definition alg4.cpp:48
void unique_example()
Definition alg4.cpp:104
int main()
Definition alg4.cpp:150
bool isEven(int n)
Definition alg4.cpp:42