Difference between revisions of "cpp/algorithm/fill"
From cppreference.com
Satishgoda (Talk | contribs) (Replaced the for loop with a range based for loop) |
(→See also: std::copy) |
||
Line 64: | Line 64: | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/algorithm/dsc fill_n}} | {{dsc inc | cpp/algorithm/dsc fill_n}} | ||
+ | {{dsc inc | cpp/algorithm/dsc copy}} | ||
{{dsc inc | cpp/algorithm/dsc generate}} | {{dsc inc | cpp/algorithm/dsc generate}} | ||
{{dsc inc | cpp/algorithm/dsc transform}} | {{dsc inc | cpp/algorithm/dsc transform}} |
Revision as of 08:01, 17 March 2015
Defined in header <algorithm>
|
||
template< class ForwardIt, class T > void fill( ForwardIt first, ForwardIt last, const T& value ); |
||
Assigns the given value
to the elements in the range [first, last)
.
Contents |
Parameters
first, last | - | the range of elements to modify |
value | - | the value to be assigned |
Type requirements |
Return value
(none)
Complexity
Exactly last - first
assignments.
Possible implementation
template< class ForwardIt, class T > void fill(ForwardIt first, ForwardIt last, const T& value) { for (; first != last; ++first) { *first = value; } } |
Example
The following code uses fill()
to set all of the elements of a vector of integers to -1:
Run this code
#include <algorithm> #include <vector> #include <iostream> int main() { int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::vector<int> v1(data, data+10); std::fill(v1.begin(), v1.end(), -1); for (auto elem : v1) { std::cout << elem << " "; } std::cout << "\n"; }
Output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
See also
copy-assigns the given value to N elements in a range (function template) | |
(C++11) |
copies a range of elements to a new location (function template) |
assigns the results of successive function calls to every element in a range (function template) | |
applies a function to a range of elements, storing results in a destination range (function template) |