[ Web Proxy ]
URL:
Viewing: https://cppreference.com/cpp/algorithm/generate_n [Back]  [Original]

std::generate_n - cppreference.com
cppreference.com
Namespaces
Variants

std::generate_n

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
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 [firststd::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:

Ret fun();

The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret.

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

#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 invocations
or 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

saves the result of N applications of a function
(algorithm function object)[edit]
copy-assigns the given value to N elements in a range
(function template & algorithm function object)[edit]
assigns the results of successive function calls to every element in a range
(function template & algorithm function object)[edit]

Web Proxy Viewer  |  New URL  |  Original Page