From cppreference.com
Defined in header
template<classOutputIt,classSize,classGenerator>OutputItgenerate_n(OutputItfirst,Sizecount,Generatorgen); (1) template<classExecutionPolicy,classForwardIt,classSize,classGenerator>ForwardItgenerate_n(ExecutionPolicy&&policy,ForwardItfirst,Sizecount,Generatorgen); (2) (since C++17)1) If count is positive, assigns the result of successive evaluations of gen() to each element in the target range [first, std::next(first,count)). Otherwise does nothing.
2) Same as (1), but executed according to policy.
This overload participates in overload resolution only if the value of the following expression is true:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(since C++20)Parameters
first - the beginning of the target range count - number of the elements to generate gen - generator function object that will be called. The signature of the function should be equivalent to the following:
Retfun();The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret.
policy - the
to use Type requirements -OutputIt must meet the requirements of
. -ForwardIt must meet the requirements of
. -Size must be
to an
. Return value
The past-the-end iterator of the target range, or first if count is non-positive.
Complexity
Given N as max(count,0):
1,2) Exactly N evaluations of gen() and N assignments.
Exceptions
2) During the execution process:
If the temporary memory resources required for parallelization are not available,
is thrown.
If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for
,
is invoked).
Possible implementation
template<classOutputIt,classSize,classGenerator>constexpr// since C++20OutputItgenerate_n(OutputItfirst,Sizecount,Generatorgen){for(Sizei=0;i<count;++i,++first)*first=gen();returnfirst;}Example
Run this code
#include<algorithm>#include<functional>#include<iostream>#include<iterator>#include<random>intmain(){std::mt19937rng;// default constructed, seeded with fixed seedstd::generate_n(std::ostream_iterator<std::mt19937::result_type>(std::cout," "),5,std::ref(rng));std::cout<<'\n';}Output:
3499211612 581869302 3890346734 3586334585 545404204 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 the complexity requirement was “exactly count invocations
or assignments”, which is broken if count is negative no invocation or assignment
if count is non-positive
C++98 the location of the first element following
the generation range was not returned returned See also
(C++20)
saves the result of N applications of a function
(algorithm function object)
copy-assigns the given value to N elements in a range
(function template & algorithm function object)
(C++20)
assigns the results of successive function calls to every element in a range
(function template & algorithm function object)
(C++20)