remove [C++ Reference]
Keywords: stl,algorithm,remove
Date: 2009-10-09T01:21:30-0700

## remove

Syntax:

        #include <algorithm>
        forward_iterator remove( forward_iterator start, forward_iterator end, const TYPE& val );

The remove algorithm removes all of the elements in the range `[start,end)` that are equal to `val`.

The return value of this function is an iterator after the last element of the new sequence that should contain no elements equal to `val`.

Note that common implementations of remove don't actually “remove” things from the range `[start, end)`; if remove is called on a container, the length of the container will remain the same afterwards (remove couldn't possibly affect that through the iterators alone), and all the elements will still be in the container. Instead, remove puts all the “removed” elements to the end of the container, and returns the iterator that separates the not-removed and removed elements. To actually remove items from a container, you would have to call the erase method of the container, to erase elements starting at the returned iterator. This is usually combined in what is called the erase-remove idiom:

    container.erase(remove(container.begin(), container.end(), val), container.end());

The remove function runs in linear time.

Related Topics: remove_copy, remove_copy_if, remove_if, unique, unique_copy