std::generate_n
From cppreference.com
| Defined in header <algorithm>
|
||
template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator gen );
|
(1) | |
template< class ExecutionPolicy,
class ForwardIt, class Size, class Generator >
ForwardIt generate_n( ExecutionPolicy&& policy,
ForwardIt first, Size count, Generator gen );
|
(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:
|
|
(until C++20) |
|
|
(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:
The type | ||||||
| policy | - | the execution policy to use | ||||||
| Type requirements | ||||||||
-OutputIt must meet the requirements of LegacyOutputIterator.
| ||||||||
-ForwardIt must meet the requirements of LegacyForwardIterator.
| ||||||||
-Size must be convertible to an integral type.
| ||||||||
Return value
The past-the-end iterator of the target range, or first if count is non-positive.
Complexity
Given \(\scriptsize N\)N as \(\scriptsize \max(\mathtt{count},0)\)max(count,0):
1,2) Exactly \(\scriptsize N\)N evaluations of
gen() and \(\scriptsize N\)N assignments.Exceptions
2) During the execution process:
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Possible implementation
template<class OutputIt, class Size, class Generator>
constexpr // since C++20
OutputIt generate_n(OutputIt first, Size count, Generator gen)
{
for (Size i = 0; i < count; ++i, ++first)
*first = gen();
return first;
}
|
Example
Run this code
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
int main()
{
std::mt19937 rng; // default constructed, seeded with fixed seed
std::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 |
|---|---|---|---|
| LWG 426 | C++98 | the complexity requirement was exactly count invocationsor assignments, which is broken if count is negative
|
no invocation or assignment if count is non-positive
|
| LWG 865 | 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) |