std::generate_n
Min standard notice:
Header: <algorithm>
- Assigns values, generated by given function object g, to the first count elements in the range beginning at first, if count > 0. Does nothing otherwise.
# Declarations
template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator g );
(constexpr since C++20)
template< class ExecutionPolicy,
class ForwardIt, class Size, class Generator >
ForwardIt generate_n( ExecutionPolicy&& policy,
ForwardIt first, Size count, Generator g );
(since C++17)
# Parameters
first: the beginning of the range of elements to generatecount: number of the elements to generatepolicy: the execution policy to useg: generator function object that will be called. The signature of the function should be equivalent to the following: Ret fun(); The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret.
# Return value
Iterator one past the last element assigned if count > 0, first otherwise.
# Example
#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';
}
# Defect reports
| 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 assignmentif count is non-positive |
| LWG 865 | C++98 | the location of the first element followingthe generation range was not returned | returned |