Silicon Graphics, Inc.

replace_if

Category: algorithms Component type: function

Prototype

template <class ForwardIterator, class Predicate, class T>
void replace(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 algo.h.

Requirements on types

Preconditions

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
[Silicon Surf] [STL Home]
Copyright © 1996 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation