Or login with:
#include <algorithm> template < class ForwardIterator, class Predicate > ForwardIterator remove_if( ForwardIterator first, ForwardIterator last, Predicate pred );Parameters:
| Parameter | Description |
|---|---|
| first | A forward iterator pointing to the position of the first element in the range from which elements are being removed |
| last | A forward iterator pointing to the position one past the final element in the range from which elements are being removed |
| pred | The unary predicate that must be satisfied is the value of an element is to be replaced |
true using a given predicate.
Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. Remove is stable, meaning that the relative order of elements that are not removed is unchanged.#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main() { vector<int> coll; INSERT_ELEMENTS(coll,2,6); INSERT_ELEMENTS(coll,4,9); INSERT_ELEMENTS(coll,1,7); PRINT_ELEMENTS(coll,"coll: "); //remove all elements with value 5 vector<int>::iterator pos; pos = remove (coll. begin(), coll.end(), //range 5); //value to remove PRINT_ELEMENTS(coll,"size not changed: "); //erase the "removed" elements in the container coll. erase (pos, coll.end()); PRINT_ELEMENTS(coll,"size changed: "); //remove all elements less than 4 coll.erase(remove_if (coll.begin(), coll.end(), //range bind2nd(less<int>(),4)), //remove criterion coll.end()); PRINT_ELEMENTS(coll,"<4 removed: : "); return 0; }
You must login to leave a messge