Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg3.cpp
Go to the documentation of this file.
1
30#include <algorithm> // for copy(), find(), generate(), ...
31#include <functional> // for greater
32#include <iostream> // for cout
33#include <iterator> // for ostream_iterator
34#include <list> // for list
35#include <string> // for string
36#include <vector> // for vector
37
38#include <ctype.h>
39#include <cstring>
40
41#include <examples.h>
42
43
44typedef std::ostream_iterator<int, char, std::char_traits<char> >
46
47
48struct iotaGenerator
49{
50 iotaGenerator (int iv): current (iv) { }
51
53 return current++;
54 }
55
56private:
57 int current;
58};
59
60
61struct RandomInteger
62{
63 static long seq [16];
64
65 long operator () (long n) const {
66 std::random_shuffle (seq, seq + sizeof seq / sizeof *seq);
67
68 const long rnd =
69 (seq [0] << 11) | (seq [1] << 8) | (seq [2] << 4) + seq [3];
70
71 return rnd % n;
72 }
73};
74
75long RandomInteger::seq [16] = {
76 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf
77};
78
79
80bool isEven (int n)
81{
82 return 0 == (n % 2);
83}
84
85// Illustrate the use of the reverse function.
87{
88 std::cout << "Illustrate std::reverse() algorithm\n";
89
90 // Example 1, reversing a std::string.
91 char text[] = "<Rats live on no evil star>";
92
93 std::cout << text << '\n';
94
95 std::reverse (text, text + sizeof text - 1);
96
97 std::cout << text << '\n';
98
99 // Example 2, reversing a std::list.
100 std::list<int, std::allocator<int> > iList;
101
102 std::generate_n (std::inserter (iList, iList.begin ()),
103 10, iotaGenerator (2));
104
105 std::copy (iList.begin (), iList.end (),
106 ostrm_iter_type (std::cout, " "));
107
108 std::cout << '\n';
109
110 std::reverse (iList.begin (), iList.end ());
111 std::copy (iList.begin (), iList.end (),
112 ostrm_iter_type (std::cout, " "));
113
114 std::cout << '\n';
115}
116
117
118// Illustrate the use of the replace function.
120{
121 std::cout << "Illustrate std::replace() algorithm\n";
122
123 // Make std::vector 0 1 2 3 4.
124 std::vector<int, std::allocator<int> > numbers (11);
125 for (std::size_t i = 0; i < numbers.size (); i++)
126 numbers [i] = int (i < 5U ? i : 10U - i);
127
128 std::copy (numbers.begin (), numbers.end (),
129 ostrm_iter_type (std::cout, " "));
130
131 std::cout << '\n';
132
133 // Replace 0 by 2.
134 std::replace (numbers.begin (), numbers.end (), 3, 7);
135 std::copy (numbers.begin (), numbers.end (),
136 ostrm_iter_type (std::cout, " "));
137
138 std::cout << '\n';
139
140 // Replace even numbers by 9.
141 std::replace_if (numbers.begin (),
142 numbers.end (), isEven, 9);
143
144 std::copy (numbers.begin (), numbers.end (),
145 ostrm_iter_type (std::cout, " "));
146
147 std::cout << '\n';
148
149 // Copy into a std::list, replastd::cing 9 by 3.
150 int aList[] = { 2, 1, 4, 3, 2, 5 };
151 int bList[6];
152 int cList[6];
153
154 std::replace_copy (aList, aList+6, &bList[0], 2, 7);
155 std::replace_copy_if (bList, bList+6, &cList[0],
156 std::bind2nd (std::greater<int> (), 3), 8);
157
158 std::copy (bList, bList + 6, ostrm_iter_type (std::cout, " "));
159 std::cout << '\n';
160
161 std::copy (cList, cList + 6, ostrm_iter_type (std::cout, " "));
162 std::cout << '\n';
163}
164
165
166// Illustrate the use of the rotate function.
168{
169 std::cout << "Illustrate std::rotate() algorithm\n";
170
171 // Create the std::list 1 2 3 ... 10
172 std::list<int, std::allocator<int> > iList;
173 std::generate_n (std::inserter (iList, iList.begin ()),
174 10, iotaGenerator (1));
175
176 // Find the location of the seven.
177 std::list<int, std::allocator<int> >::iterator
178 middle = std::find (iList.begin (), iList.end (), 7);
179
180 // Now rotate around that location.
181 std::rotate (iList.begin (), middle, iList.end ());
182 std::copy (iList.begin (), iList.end (),
183 ostrm_iter_type (std::cout, " "));
184
185 std::cout << '\n';
186
187 // Rotate again around the same location.
188 std::list<int, std::allocator<int> > cList;
189
190 std::rotate_copy (iList.begin (), middle, iList.end (),
191 std::inserter (cList, cList.begin ()));
192 std::copy (cList.begin (), cList.end (),
193 ostrm_iter_type (std::cout, " "));
194
195 std::cout << '\n';
196}
197
198
199// Illustrate the use of the paration function.
201{
202 std::cout << "Illustrate std::partition() algorithm\n";
203
204 // First make the std::vector 1 2 3 ... 10.
205 std::vector<int, std::allocator<int> > numbers (10);
206
207 std::generate (numbers.begin (),
208 numbers.end (), iotaGenerator (1));
209
210 // Now put the odd values low, even values high.
211 std::vector<int, std::allocator<int> >::iterator
212 result = std::partition (numbers.begin (), numbers.end (),
213 isEven);
214
215 std::copy (numbers.begin (), numbers.end (),
216 ostrm_iter_type (std::cout, " "));
217
218 std::cout << "\nmiddle location " << result - numbers.begin () << '\n';
219
220 // Now do a stable partition.
221 std::generate (numbers.begin (),
222 numbers.end (), iotaGenerator (1));
223
224 std::copy (numbers.begin (), numbers.end (),
225 ostrm_iter_type (std::cout, " "));
226
227 std::cout << '\n';
228}
229
230
231bool nameCompare (const char *a, const char *b)
232{
233 return std::strcmp (a, b) <= 0;
234}
235
236
237// Illustrate the use of the next_permutation function.
239{
240 std::cout << "Illustrate std::next_permutation() algorithm\n";
241
242 // Start with the values 1 2 3 in sequence.
243 int start [] = { 1, 2, 3 };
244
245 do {
246 std::copy (start, start + 3, ostrm_iter_type (std::cout, " "));
247 std::cout << '\n';
248 } while (std::next_permutation (start, start + 3));
249
250 const char alpha[] = "Alpha";
251 const char beta[] = "Beta";
252 const char gamma[] = "Gamma";
253
254 const char* names[] = { alpha, beta, gamma };
255
256 typedef std::ostream_iterator<const char*, char, std::char_traits<char> >
257 Iter;
258
259 do {
260 std::copy (names, names + 3, Iter (std::cout, " "));
261 std::cout << '\n';
262 } while (std::next_permutation (names, names + 3, nameCompare));
263
264 std::cout << "Illustrate std::prev_permutation() algorithm\n";
265
266 char word[] = "bela";
267
268 do
269 std::cout << word << ' ';
270 while (std::prev_permutation (word, &word[4]));
271
272 std::cout << '\n';
273}
274
275
276// Illustrate the use of the inplace_merge function.
278{
279 std::cout << "Illustrate std::inplace_merge() algorithm\n";
280
281 // First generate the numbers 0 2 4 6 8 1 3 5 7 9.
282 std::vector<int, std::allocator<int> > numbers (10);
283
284 for (std::size_t i = 0; i < numbers.size (); i++)
285 numbers [i] = int (i < 5U ? 2U * i : 2U * (i - 5U) + 1U);
286
287 std::copy (numbers.begin (), numbers.end (),
288 ostrm_iter_type (std::cout, " "));
289 std::cout << '\n';
290
291 std::vector<int, std::allocator<int> >::iterator
292 midvec = std::find (numbers.begin (), numbers.end (), 1);
293
294 // Copy them into a std::list.
295 std::list<int, std::allocator<int> > numList;
296 std::copy (numbers.begin (), numbers.end (),
297 std::inserter (numList, numList.begin ()));
298
299 std::list<int, std::allocator<int> >::iterator
300 midList = std::find (numList.begin (), numList.end (), 1);
301 std::copy (numList.begin (), numList.end (),
302 ostrm_iter_type (std::cout, " "));
303
304 std::cout << '\n';
305
306 // Now put them back together.
307 std::inplace_merge (numbers.begin (), midvec, numbers.end ());
308 std::inplace_merge (numList.begin (), midList, numList.end ());
309 std::copy (numList.begin (), numList.end (),
310 ostrm_iter_type (std::cout, " "));
311
312 std::cout << '\n';
313}
314
315
316// Illustrate the use of the random_shuffle function.
318{
319 std::cout << "Illustrate std::generate() algorithm\n";
320
321 // First make the std::vector 1 2 3 ... 10.
322 std::vector<int, std::allocator<int> > numbers (10);
323
324 std::generate (numbers.begin (),
325 numbers.end (), iotaGenerator (1));
326 std::copy (numbers.begin (), numbers.end (),
327 ostrm_iter_type (std::cout, " "));
328
329 std::cout << "\nIllustrate std::random_shuffle() algorithm\n";
330
331 // Randomly shuffle the elements.
332 RandomInteger rnd;
333 std::random_shuffle (numbers.begin (), numbers.end (), rnd);
334 std::copy (numbers.begin (), numbers.end (),
335 ostrm_iter_type (std::cout, " "));
336
337 std::cout << '\n';
338
339 // Do it again.
340 std::random_shuffle (numbers.begin (), numbers.end (), rnd);
341 std::copy (numbers.begin (), numbers.end (),
342 ostrm_iter_type (std::cout, " "));
343
344 std::cout << '\n';
345}
346
347
348int main ()
349{
350 std::cout << "STL generic algorithms -- in-place algorithms\n";
351
359
360 std::cout << "End of in-place algorithm sample program\n";
361
362 return 0;
363}
#define i
Definition 17.names.cpp:126
#define middle
Definition 17.names.cpp:496
#define start
Definition 17.names.cpp:547
#define a
Definition 17.names.cpp:109
#define b
Definition 17.names.cpp:110
#define n
Definition 17.names.cpp:130
volatile int result
void reverse_example()
Definition alg3.cpp:86
void partition_example()
Definition alg3.cpp:200
void rotate_example()
Definition alg3.cpp:167
void random_shuffle_example()
Definition alg3.cpp:317
void inplace_merge_example()
Definition alg3.cpp:277
void permutation_example()
Definition alg3.cpp:238
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg3.cpp:45
bool nameCompare(const char *a, const char *b)
Definition alg3.cpp:231
int main()
Definition alg3.cpp:348
void replace_example()
Definition alg3.cpp:119
bool isEven(int n)
Definition alg3.cpp:80
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
int operator()()
Definition alg3.h:40
iotaGenerator(int iv)
Definition alg3.cpp:50
int operator()(int m)
Definition alg3.h:50
static long seq[16]
Definition alg3.cpp:75