C++ examples for STL Algorithm:remove_copy
Removing values from a sequence with algorithms remove_copy
#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()// w w w . jav a 2 s.co 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 > c(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 from v2 to c, removing 10s in the process remove_copy(v.begin(), v.end(), c.begin(), 10); cout << "\nVector c after removing all 10s from v2:\n "; copy(c.begin(), c.end(), output); } // determine whether argument is greater than 9 bool greater9(int x) { return x > 9; }