Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
remove.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 copy, remove, remove_copy, remove_if
39#include <functional> // for unary_function
40#include <iostream> // for cout, endl
41#include <iterator> // for ostream_iterator
42#include <vector> // for vector
43
44
45// template<class Arg>
46// struct not_zero: public std::unary_function<Arg, bool>
47// {
48// bool operator() (const Arg &a) const {
49// return a != 0;
50// }
51// };
52
53
54typedef std::greater<int> int_greater;
55typedef std::binder2nd<int_greater> bnd_greater;
56
57int main ()
58{
59 // For convenience.
60 typedef std::vector<int, std::allocator<int> > Vector;
61 typedef std::ostream_iterator<int, char, std::char_traits<char> > Iter;
62
63 // Populate a vector with elements from an array.
64 const Vector::value_type arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
65 Vector v (arr + 0, arr + sizeof arr / sizeof *arr);
66
67 // Write out the contents to cout.
68 std::copy (v.begin (), v.end (), Iter (std::cout," "));
69 std::cout << std::endl << std::endl;
70
71 // Move the 7 to the end of the vector.
72 Vector::iterator result = std::remove (v.begin (), v.end (), 7);
73
74 // Delete the 7 from the vector.
75 v.erase (result, v.end ());
76
77 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
78 std::cout << std::endl << std::endl;
79
80 // Remove all non-zero elements beyond the fourth element.
81 v.erase (std::remove_if (v.begin () + 4, v.end (),
82 bnd_greater( int_greater (), 0 )), v.end ());
83
84 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
85 std::cout << std::endl << std::endl;
86
87 // Now remove all 3s on output.
88 std::remove_copy (v.begin (), v.end (), Iter (std::cout, " "), 3);
89 std::cout << std::endl << std::endl;
90
91 // Now remove everything satisfying predicate on output.
92 std::remove_copy_if (v.begin (), v.end (), Iter (std::cout, " "),
93 bnd_greater( int_greater (), 0 ) );
94
95 // Return 0 on success, a non-zero value on failure
96 return !!v.empty ();
97}
98
99
100
101
102
103
104
105
106
107
108
#define v
Definition 17.names.cpp:138
volatile int result
std::vector< UserClass, std::allocator< UserClass > > Vector
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
std::binder2nd< int_greater > bnd_greater
Definition remove.cpp:55
std::greater< int > int_greater
Definition remove.cpp:54
int main()
Definition remove.cpp:57