Книга: Standard Template Library Programmer
replace
Разделы на этой странице:
replace
Category: algorithms
Component type: function
Prototype
template <class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
Description
Replace replaces every element in the range [first, last) equal to old_value with new_value. That is: for every iterator i , if *i == old_value 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.
• T is convertible to ForwardIterator's value type.
• T is Assignable.
• T is EqualityComparable, and may be compared for equality with objects of ForwardIterator's value type.
Preconditions
• [first, last) is a valid range.
Complexity
Linear. Replace performs exactly last – first comparisons for equality, and at most last – first assignments.
Example
vector<int> V;
V.push_back(1);
V.push_back(2);
V.push_back(3);
V.push_back(1);
replace(V.begin(), V.end(), 1, 99);
assert(V[0] == 99 && V[3] == 99);
See also
replace_if, replace_copy, replace_copy_if
- Using the Backtick to Replace a String with Output
- 6.4.7. Page Replacement
- 3.2.6 Regular Expressions for Search and Replacement Operations
- Replace
- 3.8.47 replace
- Метод replace
- replace_if
- replace_copy
- replace_copy_if
- 3.2 Search and Replace
- 3.2.1 Simple Search and Replace Operations
- 3.2.2 Query-Replace