Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg7.cpp
Go to the documentation of this file.
1
29#include <rw/_config.h>
30
31#if defined (__IBMCPP__) && !defined (_RWSTD_NO_IMPLICIT_INCLUSION)
32// Disable implicit inclusion to work around
33// a limitation in IBM's VisualAge 5.0.2.0 (see PR#26959)
34
35# define _RWSTD_NO_IMPLICIT_INCLUSION
36#endif
37
38#include <vector>
39#include <deque>
40#include <list>
41#include <algorithm>
42#include <functional>
43#include <iostream>
44#include <iterator>
45
46#include <examples.h>
47
48
49// returns a random integer in the range [0, 100)
51{
52 static int digits[] = {
53 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
54 };
55
56 std::random_shuffle (digits, digits + sizeof digits / sizeof *digits);
57
58 const int rnd = digits [0] * 10 + digits [1];
59
60 return rnd;
61}
62
63
64typedef std::ostream_iterator<int, char, std::char_traits<char> > Iter;
65
67{
68 std::cout << "Sort algorithms\n";
69
70 Iter intOut (std::cout, " ");
71
72 // Fill both a std::vector and a std::deque with random integers.
73 std::vector<int, std::allocator<int> > aVec (15);
74 std::deque<int, std::allocator<int> > aDec (15);
75 std::generate (aVec.begin (), aVec.end (), randomValue);
76 std::generate (aDec.begin (), aDec.end (), randomValue);
77
78 // Sort the std::vector ascending.
79 std::sort (aVec.begin (), aVec.end ());
80 std::copy (aVec.begin (), aVec.end (), intOut);
81 std::cout << '\n';
82
83 // Sort the std::deque descending.
84 std::sort (aDec.begin (), aDec.end (), std::greater<int>() );
85 std::copy (aDec.begin (), aDec.end (), intOut);
86 std::cout << '\n';
87
88 // Sort the std::vector descending?
89 std::sort (aVec.rbegin (), aVec.rend ());
90 std::copy (aVec.begin (), aVec.end (), intOut);
91 std::cout << '\n';
92}
93
95{
96 std::cout << "Partial sort examples\n";
97
98 Iter intOut (std::cout, " ");
99
100 // Make a std::vector of 15 random integers.
101 std::vector<int, std::allocator<int> > aVec (15);
102 std::generate (aVec.begin (), aVec.end (), randomValue);
103 std::copy (aVec.begin (), aVec.end (), intOut);
104 std::cout << '\n';
105
106 // Partial sort the first seven positions.
107 std::partial_sort (aVec.begin (), aVec.begin () + 7, aVec.end ());
108 std::copy (aVec.begin (), aVec.end (), intOut);
109 std::cout << '\n';
110
111 // Make a std::list of random integers.
112 std::list<int, std::allocator<int> > aList (15);
113 std::generate (aList.begin (), aList.end (), randomValue);
114 std::copy (aList.begin (), aList.end (), intOut);
115 std::cout << '\n';
116
117 // Sort only the first seven elements.
118 std::vector<int, std::allocator<int> > start (7);
119 std::partial_sort_copy (aList.begin (), aList.end (),
120 start.begin (), start.end (), std::greater<int>());
121 std::copy (start.begin (), start.end (), intOut);
122 std::cout << '\n';
123}
124
125
126// Illustrate the use of the nth_largest function.
128{
129 std::cout << "Nth largest example\n";
130
131 // Make a std::vector of random integers.
132 std::vector<int, std::allocator<int> > aVec (10);
133 std::generate (aVec.begin (), aVec.end (), randomValue);
134
135 Iter intOut (std::cout, " ");
136
137 // Now find the 5th largest.
138 std::vector<int, std::allocator<int> >::iterator nth = aVec.begin () + 4;
139 std::nth_element (aVec.begin (), nth, aVec.end ());
140 std::cout << "fifth largest is " << *nth << " in\n";
141 std::copy (aVec.begin (), aVec.end (), intOut);
142 std::cout << '\n';
143}
144
145
146// Illustrate the use of the binary search functions.
148{
149 std::cout << "Binary search example\n";
150
151 Iter intOut (std::cout, " ");
152
153 // Make an ordered std::vector of 15 random integers.
154 std::vector<int, std::allocator<int> > aVec (15);
155 std::generate (aVec.begin (), aVec.end (), randomValue);
156 std::sort (aVec.begin (), aVec.end ());
157 std::copy (aVec.begin (), aVec.end (), intOut), std::cout << '\n';
158
159 // See if it contains an eleven.
160 if (std::binary_search (aVec.begin (), aVec.end (), 11))
161 std::cout << "contains an 11\n";
162 else
163 std::cout << "does not contain an 11\n";
164
165 // Insert an 11 and a 14.
166 std::vector<int, std::allocator<int> >::iterator where;
167 where = std::lower_bound (aVec.begin (), aVec.end (), 11);
168 aVec.insert (where, 11);
169 where = std::upper_bound (aVec.begin (), aVec.end (), 14);
170 aVec.insert (where, 14);
171 std::copy (aVec.begin (), aVec.end (), intOut), std::cout << '\n';
172}
173
174
175// Illustrate the use of the merge function.
177{
178 std::cout << "Merge algorithm examples\n";
179
180 // Make a std::list and std::vector of 10 random integers.
181 std::vector<int, std::allocator<int> > aVec (10);
182 std::list<int, std::allocator<int> > aList (10);
183 std::generate (aVec.begin (), aVec.end (), randomValue);
184 std::sort (aVec.begin (), aVec.end ());
185 std::generate_n (aList.begin (), 10, randomValue);
186 aList.sort ();
187
188 // Merge into a std::vector.
189 std::vector<int, std::allocator<int> > vResult (aVec.size () + aList.size ());
190 std::merge (aVec.begin (), aVec.end (), aList.begin (), aList.end (),
191 vResult.begin ());
192
193 // Merge into a std::list.
194 std::list<int, std::allocator<int> > lResult;
195 std::merge (aVec.begin (), aVec.end (), aList.begin (), aList.end (),
196 std::inserter (lResult, lResult.begin ()));
197
198 // Merge into the output.
199 std::merge (aVec.begin (), aVec.end (), aList.begin (), aList.end (),
200 Iter (std::cout, " "));
201
202 std::cout << '\n';
203}
204
205class iotaGen
206{
207public:
208 iotaGen (int c = 0) : current (c) { }
209 int operator () () { return current++; }
210 private:
211 int current;
212};
213
214
215// Illustrate the use of the generic set functions.
217{
218 std::cout << "Set operations:\n";
219
220 // Make a couple of ordered std::lists.
221 std::list <int, std::allocator<int> > listOne, listTwo;
222 std::generate_n (std::inserter (listOne, listOne.begin ()), 5, iotaGen (1));
223 std::generate_n (std::inserter (listTwo, listTwo.begin ()), 5, iotaGen (3));
224
225 Iter intOut (std::cout, " ");
226
227 // union - 1 2 3 4 5 6 7
228 std::set_union (listOne.begin (), listOne.end (),
229 listTwo.begin (), listTwo.end (), intOut);
230 std::cout << '\n';
231
232 // merge - 1 2 3 3 4 4 5 5 6 7
233 std::merge (listOne.begin (), listOne.end (),
234 listTwo.begin (), listTwo.end (), intOut);
235 std::cout << '\n';
236
237 // intersection 3 4 5
238 std::set_intersection (listOne.begin (), listOne.end (),
239 listTwo.begin (), listTwo.end (), intOut);
240 std::cout << '\n';
241
242 // difference 1 2
243 std::set_difference (listOne.begin (), listOne.end (),
244 listTwo.begin (), listTwo.end (), intOut);
245 std::cout << '\n';
246
247 // symmetric difference 1 2 6 7
248 std::set_symmetric_difference (listOne.begin (), listOne.end (),
249 listTwo.begin (), listTwo.end (),
250 intOut);
251 std::cout << '\n';
252
253 if (std::includes (listOne.begin (), listOne.end (),
254 listTwo.begin (), listTwo.end ()))
255 std::cout << "set is subset\n";
256 else
257 std::cout << "set is not subset\n";
258}
259
260
261// Illustrate the use of the heap functions.
263{
264 Iter intOut (std::cout, " ");
265
266 // Make a heap of 15 random integers.
267 std::vector<int, std::allocator<int> > aVec (15);
268 std::generate (aVec.begin (), aVec.end (), randomValue);
269 std::make_heap (aVec.begin (), aVec.end ());
270 std::copy (aVec.begin (), aVec.end (), intOut);
271 std::cout << "\nLargest value " << aVec.front () << '\n';
272
273 // Remove largest and reheap.
274 std::pop_heap (aVec.begin (), aVec.end ());
275 aVec.pop_back ();
276 std::copy (aVec.begin (), aVec.end (), intOut);
277 std::cout << '\n';
278
279 // Add a 97 to the heap.
280 aVec.push_back (97);
281 std::push_heap (aVec.begin (), aVec.end ());
282 std::copy (aVec.begin (), aVec.end (), intOut);
283 std::cout << '\n';
284
285 // Finally, make into sorted collection.
286 std::sort_heap (aVec.begin (), aVec.end ());
287 std::copy (aVec.begin (), aVec.end (), intOut);
288 std::cout << '\n';
289}
290
291int main ()
292{
293 std::cout << "Sorting generic algorithms examples\n";
294
295 sortExample ();
299 merge_example ();
300 set_example ();
301 heap_example ();
302
303 std::cout << "End sorting examples\n";
304
305 return 0;
306}
#define start
Definition 17.names.cpp:547
#define c
Definition 17.names.cpp:111
int randomValue()
Definition alg7.cpp:50
void nth_element_example()
Definition alg7.cpp:127
void merge_example()
Definition alg7.cpp:176
void heap_example()
Definition alg7.cpp:262
void partial_sort_example()
Definition alg7.cpp:94
void binary_search_example()
Definition alg7.cpp:147
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
void set_example()
Definition alg7.cpp:216
void sortExample()
Definition alg7.cpp:66
int main()
Definition alg7.cpp:291
int operator()()
Definition alg1.cpp:53
iotaGen(int c=0)
Definition alg7.cpp:208