Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library Reference Guide


Library:  Algorithms


Local Index

No Entries


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:


Applying the remove_if() algorithm results in the following sequence:


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:



Exactly finish - start applications of the corresponding predicate are done.


See Also

remove(), remove_copy(), remove_copy_if()

Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.7

Previous fileTop of DocumentContentsIndex pageNext file