function template
<algorithm>
std::generate
template <class ForwardIterator, class Generator> void generate (ForwardIterator first, ForwardIterator last, Generator gen);
Generate values for range with function
[first,last).The behavior of this function template is equivalent to:
|
|
Parameters
- first, last
- Forward iterators to the initial and final positions in a sequence. The range affected is
[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. - gen
- Generator function that is called with no arguments and returns some value of a type convertible to those pointed by the iterators.
This can either be a function pointer or a function object.
Return value
noneExample
|
|
A possible output:
myvector contains: 57 87 76 66 85 54 17 15 myvector contains: 1 2 3 4 5 6 7 8
Complexity
Linear in the distance between first and last: Calls gen and performs an assignment for each element.Data races
The objects in the range[first,last) are modified (each object is accessed exactly once).Exceptions
Throws if any of gen, the element assignments or the operations on iterators throws.Note that invalid arguments cause undefined behavior.
See also
- generate_n
- Generate values for sequence with function (function template)
- fill
- Fill range with value (function template)
- for_each
- Apply function to range (function template)