Name
generate_n function template — Fills a counted range with values returned from a function
Synopsis
template<typename OutIter, typename Size, typename Generator>
void generate_n(OutIter first, Size n, Generator gen);
The generate_n
function
template calls gen( )
exactly
n
times, assigning the results to
fill the sequence that starts at first
. You must ensure that the sequence
has room for at least n
items.
The Size
type must be convertible
to an integral type.
Example
Example 13-3 shows a simple way to print a sequence of integers.
#include <algorithm> #include <iostream> #include <iterator> // Use the same series template fromExample 13-2. int main( ) { // Print integers from 1 to 10. std::generate_n(std::ostream_iterator<int>(std::cout,"\n"), 10, series<int>(1)); }
Technical Notes
Complexity is linear: gen
is called exactly n
times.
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.