Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
replace.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 <algorithm> // for replace, replace_if, replace_copy, ...
39#include <vector> // for vector
40#include <iterator> // for ostream_iterator
41#include <functional> // for not1, unary_function
42#include <iostream> // for cout, endl
43
44#include <examples.h>
45#include <replace.h>
46
47
48int main ()
49{
50 typedef std::vector<short, std::allocator<short> > Vector;
51 typedef std::ostream_iterator<short, char, std::char_traits<char> > Iter;
52
53 // Populate a vector with arbitrary values.
54 Vector v;
55 for (Vector::value_type n = 11111; n != 11211; ++n)
56 v.push_back (n);
57
58 // Print out original vector.
59 std::cout << "Original sequence:\n ";
60 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
61 std::cout << std::endl << std::endl;
62
63 // Replace one number with another.
64 std::replace (v.begin (), v.end (), 11199, 11211);
65
66 // Print out the new vector.
67 std::cout << "Sequence after replace:\n ";
68 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
69 std::cout << std::endl << std::endl;
70
71 // Replace all numbers that aren't primes with zeros.
72 std::replace_if (v.begin (), v.end (), std::not1 (is_prime ()), 0);
73
74 // Print out the remaining vector.
75 std::cout << "After replace_if:\n ";
76 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
77 std::cout << std::endl << std::endl;
78
79 // Replace zeros with ones.
80 std::cout << "Sequence replace_copy-ed to cout:\n ";
81 std::replace_copy (v.begin (), v.end (), Iter (std::cout, " "), 0, 1);
82 std::cout << std::endl << std::endl;
83
84 // A simple example of replace_copy_if.
85 std::cout << "Sequence replace_copy_if-ed to cout:\n ";
86 std::replace_copy_if (v.begin (), v.end (), Iter (std::cout, ""),
87 is_prime (), 1);
88 std::cout << std::endl;
89
90 return 0;
91}
#define v
Definition 17.names.cpp:138
#define n
Definition 17.names.cpp:130
std::vector< UserClass, std::allocator< UserClass > > Vector
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
int main()
Definition replace.cpp:48