Library: Algorithms
Function
Algorithm that moves all occurrences of elements that satisfy the given predicate from a range to the end of the range, and returns an iterator pointing to the first moved occurrence
#include <algorithm> namespace std { template <class ForwardIterator, class Predicate> ForwardIterator remove_if(ForwardIterator start, ForwardIterator finish, Predicate pred); }
The remove_if() algorithm eliminates all the elements referred to by iterator i in the range [start, finish) for which the following corresponding condition holds: pred(*i) == true. The remove_copy_if() algorithm returns an iterator that points to the end of the resulting range. The remove_copy_if() algorithm is stable, which means that the relative order of the elements that are not removed is the same as their relative order in the original range.
The remove_copy_if() algorithm does not actually reduce the size of the sequence. It 1) copies the values that are to be retained to the front of the sequence, and 2) returns an iterator that describes where the sequence of retained values ends. Elements that follow this iterator are simply the original sequence values, left unchanged. Here's a simple example:
Say we want to remove all even numbers from the following sequence:
123456789
Applying the remove_if() algorithm results in the following sequence:
13579|XXXX
The vertical bar represents the position of the iterator returned by remove_if(). Note that the elements to the left of the vertical bar are the original sequence with the even numbers removed. The elements to the right of the bar are simply the untouched original members of the original sequence.
If you want to actually delete items from a container, use the following technique:
container.erase(remove(start,finish,value),container.end());
Exactly finish - start applications of the corresponding predicate are done.
// // remove.cpp // #include <algorithm> // for copy, remove, remove_copy, remove_if #include <functional> // for unary_function #include <iostream> // for cout, endl #include <iterator> // for ostream_iterator #include <vector> // for vector template<class Arg> struct not_zero: public std::unary_function<Arg, bool> { bool operator() (const Arg &a) const { return a != 0; } }; int main () { // For convenience. typedef std::vector<int, std::allocator<int> > Vector; typedef std::ostream_iterator<int, char, std::char_traits<char> > Iter; // Populate a vector with elements from an array. const Vector::value_type arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Vector v (arr + 0, arr + sizeof arr / sizeof *arr); // Write out the contents to cout. std::copy (v.begin (), v.end (), Iter (std::cout," ")); std::cout << std::endl << std::endl; // Move the 7 to the end of the vector. Vector::iterator result = std::remove (v.begin (), v.end (), 7); // Delete the 7 from the vector. v.erase (result, v.end ()); std::copy (v.begin (), v.end (), Iter (std::cout, " ")); std::cout << std::endl << std::endl; // Remove all non-zero elements beyond the fourth element. v.erase (std::remove_if (v.begin () + 4, v.end (), not_zero<int> ()), v.end ()); std::copy (v.begin (), v.end (), Iter (std::cout, " ")); std::cout << std::endl << std::endl; // Now remove all 3s on output. std::remove_copy (v.begin (), v.end (), Iter (std::cout, " "), 3); std::cout << std::endl << std::endl; // Now remove everything satisfying predicate on output. std::remove_copy_if (v.begin (), v.end (), Iter (std::cout, " "), not_zero<int>()); // Return 0 on success, a non-zero value on failure return !!v.empty (); } Program Output:
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 8 9 10 1 2 3 4 1 2 4
remove(), remove_copy(), remove_copy_if()
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.7