44typedef std::ostream_iterator<int, char, std::char_traits<char> >
66 std::random_shuffle (
seq,
seq +
sizeof seq /
sizeof *
seq);
69 (
seq [0] << 11) | (
seq [1] << 8) | (
seq [2] << 4) +
seq [3];
76 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf
88 std::cout <<
"Illustrate std::reverse() algorithm\n";
91 char text[] =
"<Rats live on no evil star>";
93 std::cout << text <<
'\n';
95 std::reverse (text, text +
sizeof text - 1);
97 std::cout << text <<
'\n';
100 std::list<int, std::allocator<int> > iList;
102 std::generate_n (std::inserter (iList, iList.begin ()),
105 std::copy (iList.begin (), iList.end (),
110 std::reverse (iList.begin (), iList.end ());
111 std::copy (iList.begin (), iList.end (),
121 std::cout <<
"Illustrate std::replace() algorithm\n";
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);
128 std::copy (numbers.begin (), numbers.end (),
134 std::replace (numbers.begin (), numbers.end (), 3, 7);
135 std::copy (numbers.begin (), numbers.end (),
141 std::replace_if (numbers.begin (),
142 numbers.end (),
isEven, 9);
144 std::copy (numbers.begin (), numbers.end (),
150 int aList[] = { 2, 1, 4, 3, 2, 5 };
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);
169 std::cout <<
"Illustrate std::rotate() algorithm\n";
172 std::list<int, std::allocator<int> > iList;
173 std::generate_n (std::inserter (iList, iList.begin ()),
177 std::list<int, std::allocator<int> >::iterator
178 middle = std::find (iList.begin (), iList.end (), 7);
181 std::rotate (iList.begin (),
middle, iList.end ());
182 std::copy (iList.begin (), iList.end (),
188 std::list<int, std::allocator<int> > cList;
190 std::rotate_copy (iList.begin (),
middle, iList.end (),
191 std::inserter (cList, cList.begin ()));
192 std::copy (cList.begin (), cList.end (),
202 std::cout <<
"Illustrate std::partition() algorithm\n";
205 std::vector<int, std::allocator<int> > numbers (10);
207 std::generate (numbers.begin (),
211 std::vector<int, std::allocator<int> >::iterator
212 result = std::partition (numbers.begin (), numbers.end (),
215 std::copy (numbers.begin (), numbers.end (),
218 std::cout <<
"\nmiddle location " <<
result - numbers.begin () <<
'\n';
221 std::generate (numbers.begin (),
224 std::copy (numbers.begin (), numbers.end (),
233 return std::strcmp (
a,
b) <= 0;
240 std::cout <<
"Illustrate std::next_permutation() algorithm\n";
243 int start [] = { 1, 2, 3 };
248 }
while (std::next_permutation (
start,
start + 3));
250 const char alpha[] =
"Alpha";
251 const char beta[] =
"Beta";
252 const char gamma[] =
"Gamma";
254 const char* names[] = { alpha, beta, gamma };
256 typedef std::ostream_iterator<const char*, char, std::char_traits<char> >
260 std::copy (names, names + 3,
Iter (std::cout,
" "));
262 }
while (std::next_permutation (names, names + 3,
nameCompare));
264 std::cout <<
"Illustrate std::prev_permutation() algorithm\n";
266 char word[] =
"bela";
269 std::cout << word <<
' ';
270 while (std::prev_permutation (word, &word[4]));
279 std::cout <<
"Illustrate std::inplace_merge() algorithm\n";
282 std::vector<int, std::allocator<int> > numbers (10);
284 for (std::size_t
i = 0;
i < numbers.size ();
i++)
285 numbers [
i] =
int (
i < 5U ? 2U *
i : 2U * (
i - 5U) + 1U);
287 std::copy (numbers.begin (), numbers.end (),
291 std::vector<int, std::allocator<int> >::iterator
292 midvec = std::find (numbers.begin (), numbers.end (), 1);
295 std::list<int, std::allocator<int> > numList;
296 std::copy (numbers.begin (), numbers.end (),
297 std::inserter (numList, numList.begin ()));
299 std::list<int, std::allocator<int> >::iterator
300 midList = std::find (numList.begin (), numList.end (), 1);
301 std::copy (numList.begin (), numList.end (),
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 (),
319 std::cout <<
"Illustrate std::generate() algorithm\n";
322 std::vector<int, std::allocator<int> > numbers (10);
324 std::generate (numbers.begin (),
326 std::copy (numbers.begin (), numbers.end (),
329 std::cout <<
"\nIllustrate std::random_shuffle() algorithm\n";
333 std::random_shuffle (numbers.begin (), numbers.end (), rnd);
334 std::copy (numbers.begin (), numbers.end (),
340 std::random_shuffle (numbers.begin (), numbers.end (), rnd);
341 std::copy (numbers.begin (), numbers.end (),
350 std::cout <<
"STL generic algorithms -- in-place algorithms\n";
360 std::cout <<
"End of in-place algorithm sample program\n";
void random_shuffle_example()
void inplace_merge_example()
void permutation_example()
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
bool nameCompare(const char *a, const char *b)
std::ostream_iterator< int, char, std::char_traits< char > > Iter