std::generate(3)
std::generate(3) β straight from the C++ Standard Libary, and yes bestie, this is the whole page about it πβ¨
πΈ NAME πΈ
std::generate β std::generate (itβs giving βname so nice they said it twiceβ, no cap π
)
β¨ Synopsis β¨
Defined in header <algorithm> β donβt forget it, bestie, or your build is gonna be like ??? π
template< class ForwardIt, class Generator >
void generate( ForwardIt first, ForwardIt last, Generator g ); (1) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt, class Generator >
void generate( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Generator g ); (2) (since C++17)
- Assigns each element in range
[first, last)a value generated by the given function objectg. Literally every element gets a fresh little gift fromgπ - Same as (1), but executed according to
policy. Slay, but parallel-ish vibes.
This overload participates in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. (until C++20) And if std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. (since C++20) β yes, they changed the trait, bestie, keep up π
π Parameters π
first,lastβ the range of elements to generate.policyβ the execution policy to use. See execution policy for details.gβ 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 ForwardIt can be dereferenced and assigned a value of type Ret. So like, g hands you a Ret, and the iterator has to be able to take it. Periodt π
π§ Type requirements π§
ForwardItmust meet the requirements ofLegacyForwardIterator. No fake iterators allowed, bestie π«
π₯ Complexity π₯
Exactly std::distance(first, last) invocations of g() and assignments. Thatβs the whole budget β no extra credit, no sneaky bonus calls π₯
π Exceptions π
The overload with a template parameter named ExecutionPolicy reports errors as follows β and this is the tea β:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. (Yikes, bestie π) - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π Possible implementation π
Hereβs the vibe of how it can be implemented β not mandatory, just iconic:
template<class ForwardIt, class Generator>
constexpr //< since C++20
void generate(ForwardIt first, ForwardIt last, Generator g)
{
for (; first != last; ++first)
*first = g();
}
π Example π
Run this code, bestie β itβs a whole little demo:
// Run this code
#include <algorithm>
#include <iostream>
#include <vector>
void println(std::string_view fmt, const auto& v)
{
for (std::cout << fmt; const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
};
int f()
{
static int i;
return ++i;
}
int main()
{
std::vector<int> v(5);
std::generate(v.begin(), v.end(), f);
println("v: ", v);
// Initialize with default values 0,1,2,3,4 from a lambda function
// Equivalent to std::iota(v.begin(), v.end(), 0);
std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
println("v: ", v);
}
π Output π
v: 1 2 3 4 5
v: 0 1 2 3 4
π¦ See also π¦
fillβ copy-assigns the given value to every element in a range (function template)generate_nβ assigns the results of successive function calls to N elements in a range (function template)iota(C++11) β fills a range with successive increments of the starting value (function template)ranges::generate(C++20) β saves the result of a function in a range (niebloid)
Mwah π β source/version/date check: http://cppreference.com Β· 2024.06.10 Β· std::generate(3). Now go generate something iconic, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms