stlab.adobe.com Adobe Systems Incorporated

replace_if

algorithms.gif
function.gif
Category: algorithms Component type: function

Prototype

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

Description

Replace_if replaces every element in the range [first, last) for which pred returns true with new_value. That is: for every iterator i, if pred(*i) is true then it performs the assignment *i = new_value.

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

  • ForwardIterator is a model of ForwardIterator.
  • ForwardIterator is mutable.
  • Predicate is a model of Predicate.
  • ForwardIterator's value type is convertible to Predicate's argument type.
  • T is convertible to Forward Iterator's value type.
  • T is Assignable.

Preconditions

  • [first, last) is a valid range.

Complexity

Linear. Replace_if performs exactly last - first applications of pred, and at most last - first assignments.

Example

Replace every negative number with 0.

Vector<int> V;
V.push_back(1);
V.push_back(-3);
V.push_back(2);
V.push_back(-1);

replace_if(V.begin(), V.end(), bind2nd(less<int>(), 0), -1);
assert(V[1] == 0 && V[3] == 0);

Notes

See also

replace, replace_copy, replace_copy_if

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google