next up previous index
Next: Fill a range Up: Mutating sequence operations Previous: Transform a sequence

Replace specified elements of a sequence

Source code

Declaration

template <class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value,
             const T& new_value);

template <class ForwardIterator, class Predicate, class T>
void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred,
                const T& new_value);

template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy(InputIterator first, InputIterator last,
                            OutputIterator result, const T& old_value,
                            const T& new_value);

template <class InputIterator, class OutputIterator, class Predicate, class T>
OutputIterator replace_copy_if(InputIterator first, InputIterator last,
                               OutputIterator result, Predicate pred,
                               const T& new_value);

Description  The replace  function modifies the range [ first, last) so that all elements equal to old_value are replaced  by new_value, while other values remain unchanged.

The replace_if  function modifies the range [ first, last) so that all elements that make the predicate  pred return true (i.e., a nonzero value) are replaced by new_value, while other values remain unchanged.

The replace_copy  and replace_copy_if  functions are similar to the replace  and replace_if  functions, except that the original sequence is not altered, with the altered sequence being placed in the range of size n beginning at result .

Type requirements

Group Mutating sequence operations.

Time complexity Linear.

The number of equality operations performed, or of applications of the predicate pred, is the size of the range [ first, last) .

Space complexity Constant.

Mutative? Yes.

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.



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