next up previous index
Next: Remove consecutive duplicates Up: Mutating sequence operations Previous: Fill a range

Remove elements from a sequence

Source code

Declaration

template <class ForwardIterator, class T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last,
                       const T& value);

template <class ForwardIterator, class Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
                          Predicate pred);

template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy(InputIterator first, InputIterator last,
                           OutputIterator result, const T& value);

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator remove_copy_if(InputIterator first, InputIterator last,
                              OutputIterator result, Predicate pred);

Description    The function remove  removes  those elements from the range [ first, last) that are equal to value and returns the location l, the past-the-end iterator .

The function remove_if  removes  those elements from the range [ first, last) that make the predicate pred return true (i.e, a nonzero value) and returns the location l, the past-the-end iterator .

The function remove_copy  copies  all elements from the range [ first, last) to the range [ result, l) , except those that are equal to value, and returns the location l that is the past-the-end iterator  for the resulting sequence.

The function remove_copy_if  copies  all elements from the range [ first, last) to the range [ result, l) , except those that make predicate pred return true (i.e., a nonzero value), and returns the location l that is the past-the-end iterator  for the resulting sequence.

Type requirements

Group Mutating sequence operations.

Time complexity Linear.

The number of assignments is the number of elements not removed, at most n, where n is the size of the range [ first, last) .

Space complexity Constant.

Mutative? Yes.

Details These functions are stable; i.e., the elements that remain in the range (ie. [ first, l) for replace  and replace_if , and [ result, l) for replace_copy  and replace_copy_if ) are in the same order as they were in the original range.

Example Select here to view the source code for the example.

 

Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.



next up previous index
Next: Remove consecutive duplicates Up: Mutating sequence operations Previous: Fill a range



Kenny Zalewski
Mon May 13 04:03:40 EDT 1996