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


Library:  Algorithms


Local Index

No Entries


Algorithm that moves all occurrences of a value 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 T>
  remove(ForwardIterator start,
         ForwardIterator finish,
         const T& value);


The remove() algorithm eliminates all the elements referred to by iterator i in the range [start, finish) for which the following condition holds: *i == value. remove() returns an iterator that points to the end of the resulting range. remove() 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.

remove() does not actually reduce the size of the sequence. It actually: 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 values of 2 from the following sequence:


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


The vertical bar represents the position of the iterator returned by remove(). Note that the elements to the left of the vertical bar are the original sequence with the 2s removed.

If you want to actually delete items from the container, use the following technique:



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


See Also

remove_if(), 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