Calculate the sum of two vectors, storing the result in a third vector.
const int N = 1000;
vector<int> V1(N);
vector<int> V2(N);
vector <int> V3(N);
iota(V1.begin(), V1.end(), 1);
fill(V2.begin(), V2.end(), 75);
assert(V2.size() >= V1.size() && V3.size() >= V1.size());
transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), plus <int>());
Notes [1] The Output Iterator result is not permitted to be the same as any of the Input Iterators in the range [first, last), with the exception of first itself. That is: transform(V.begin(), V.end(), V.begin(), fabs) is valid, but transform(V.begin(), V.end(), V.begin() + 1, fabs) is not.
See also The function object overview, copy, generate, fill
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
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