C++ examples for STL Algorithm:remove_copy_if
Removing values from a sequence with algorithms remove_copy_if.
#include <iostream> #include <algorithm> // algorithm definitions #include <vector> // vector class-template definition #include <iterator> // ostream_iterator using namespace std; bool greater9(int); // prototype int main()//ww w . j a va 2s . c o m { const int SIZE = 10; int a[SIZE] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; ostream_iterator< int > output(cout, " "); vector< int > v(a, a + SIZE); // copy of a vector< int > v4(a, a + SIZE); // copy of a vector< int > c2(a, a + SIZE); // copy of a vector< int >::iterator newLastElement; cout << "Vector v before removing all 10s:\n "; copy(v.begin(), v.end(), output); // copy elements from v4 to c2, removing elements greater // than 9 in the process remove_copy_if(v4.begin(), v4.end(), c2.begin(), greater9); cout << "\nVector c2 after removing all elements" << "\ngreater than 9 from v4:\n "; copy(c2.begin(), c2.end(), output); cout << endl; } // determine whether argument is greater than 9 bool greater9(int x) { return x > 9; }