Книга: Standard Template Library Programmer
replace_if
Разделы на этой странице:
replace_if
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 Forward Iterator.
• 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);
See also
replace, replace_copy, replace_copy_if