Preconditions
• [first, last) is a valid range.
• There is enough space in the output range to store the copied values. That is, [result, result + (last-first)) is a valid range.
• result is not an iterator within the range [first, last) .
Complexity
Linear. Replace_copy performs exactly last – first comparisons for equality and exactly last – first assignments.
Example
vector V1;
V1.push_back(1);
V1.push_back(2);
V1.push_back(3);
V1.push_back(1);
vector V2(4);
replace_copy(V1.begin(), V1.end(), V2.begin(), 1, 99);
assert(V[0] == 99 && V[1] == 2 && V[2] == 3 && V[3] == 99);
See also
copy , replace , replace_if , replace_copy_if
Category: algorithms
Component type: function
Prototype
template
OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value)
Description
Replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)) , except that any element for which pred is true is not copied; new_value is copied instead. More precisely, for every integer n such that 0 <= n < last-first , replace_copy_if performs the assignment *(result+n) = new_value if pred(*(first+n)) , and *(result+n) = *(first+n) otherwise.
Definition
Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
Requirements on types
• InputIterator is a model of Input Iterator.
• OutputIterator is a model of Output Iterator.
• Predicate is a model of Predicate.
• T is convertible to Predicate 's argument type.
• T is Assignable.
• T is convertible to a type in OutputIterator 's set of value types.
Preconditions
• [first, last) is a valid range.
• There is enough space in the output range to store the copied values. That is, [result, result + (last-first)) is a valid range.
• result is not an iterator within the range [first, last) .
Complexity
Linear. Replace_copy performs exactly last – first applications of pred and exactly last – first assignments.
Example
Copy elements from one vector to another, replacing all negative numbers with 0 .
vector V1;
V1.push_back(1);
V1.push_back(-1);
V1.push_back(-5);
V1.push_back(2);
vector V2(4);
replace_copy_if(V1.begin(), V1.end(), V2.begin(), bind2nd(less(), 0), 0);
assert(V[0] == 1 && V[1] == 0 && V[2] == 0 && V[3] == 2);
See also
copy , replace , replace_if , replace_copy
Category: algorithms
Component type: function
Prototype
template
void fill(ForwardIterator first, ForwardIterator last, const T& value);
Description
Fill assigns the value value to every element in the range [first, last) . That is, for every iterator i in [first, last) , it performs the assignment *i = 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 . [1]
• ForwardIterator is mutable.
• T is a model of Assignable.
• T is convertible to Forward Iterator's value type.
Preconditions
• [first, last) is a valid range.
Complexity
Linear. Fill performs exactly last – first assignments.
Example
vector V(4);
fill(V.begin(), V.end(), 137);
assert(V[0] == 137 && V[1] == 137 && V[2] == 137 && V[3] == 137);
Notes
[1] The reason that fill requires its argument to be a mutable forward iterator, rather than merely an output iterator, is that it uses a range [first, last) of iterators. There is no sensible way to describe a range of output iterators, because it is impossible to compare two output iterators for equality. The fill_n algorithm does have an interface that permits use of an output iterator.
See also
copy , fill_n , generate , generate_n , iota
Category: algorithms
Component type: function
Prototype
template
OutputIterator fill_n(OutputIterator first, Size n, const T& value);
Description
Fill_n assigns the value value to every element in the range [first, first+n) . That is, for every iterator i in [first, first+n) , it performs the assignment *i = value . The return value is first + n .
Definition
Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
Requirements on types
• OutputIterator is a model of Output Iterator.
• Size is an integral type (either signed or unsigned).
• T is a model of Assignable.
• T is convertible to a type in OutputIterator 's set of value types.
Preconditions
• n >= 0 .
• There is enough space to hold n values. That is, [first, first+n) is a valid range.
Complexity
Linear. Fill_n performs exactly n assignments.
Example
vector V;
fill_n(back_inserter(V), 4, 137);
assert(V.size() == 4 && V[0] == 42 && V[1] == 42 && V[2] == 42 && V[3] == 42);
See also
copy , fill , generate , generate_n , iota
Читать дальше