C++ examples for STL Algorithm:remove_if
Removing values from a sequence with algorithms remove_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()//from w w w. java2 s . com { 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 >::iterator newLastElement; cout << "Vector v before removing all 10s:\n "; copy(v.begin(), v.end(), output); vector< int > v2(a, a + SIZE); // copy of a vector< int > c(SIZE, 0); // instantiate vector c cout << "\n\nVector v2 before removing all 10s and copying :\n "; copy(v2.begin(), v2.end(), output); // remove elements greater than 9 from v3 newLastElement = remove_if(v2.begin(), v2.end(), greater9); cout << "\nVector v3 after removing all elements" << "\ngreater than 9:\n "; copy(v2.begin(), newLastElement, output); } // determine whether argument is greater than 9 bool greater9(int x) { return x > 9; }