Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
std::ranges
Non-modifying sequence operations | |||||||||||||||||||||||||||||||||
Modifying sequence operations | |||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||
Sorting operations | |||||||||||||||||||||||||||||||||
Binary search operations (on sorted ranges) | |||||||||||||||||||||||||||||||||
Set operations (on sorted ranges) | |||||||||||||||||||||||||||||||||
Heap operations | |||||||||||||||||||||||||||||||||
Minimum/maximum operations | |||||||||||||||||||||||||||||||||
Permutation operations | |||||||||||||||||||||||||||||||||
Fold operations | |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
Operations on uninitialized storage | |||||||||||||||||||||||||||||||||
Return types | |||||||||||||||||||||||||||||||||
Defined in header <random> | ||
Call signature | ||
template<class R,class G> requiresranges::output_range<R,std::invoke_result_t<G&>>&& | (1) | (since C++26) |
template<class G,std::output_iterator<std::invoke_result_t<G&>> O, std::sentinel_for<O> S> | (2) | (since C++26) |
template<class R,class G,class D> requiresranges::output_range<R,std::invoke_result_t<D&, G&>>&& | (3) | (since C++26) |
template<class G,class D,std::output_iterator<std::invoke_result_t<D&, G&>> O, std::sentinel_for<O> S> | (4) | (since C++26) |
Attempts to generate random numbers with thegenerate_random
member function of the random number generator or the distribution, which is expected to be more efficient. Falls back to element-wise generation if nogenerate_random
member function is available.
Let fallback operation be callingranges::generate(std::forward<R>(r),std::ref(g)) orranges::generate(std::forward<R>(r),[&d,&g]{returnstd::invoke(d, g);}) for(1) or(3) respectively.
R
modelssized_range
, fillsr withranges::size(r) values ofI by performing an unspecified number of invocations of the formg() org.generate_random(s), if such an expression is well-formed for a valueN
and an objects of typestd::span<I, N>.R
modelssized_range
, fillsr withranges::size(r) values of typeI by performing an unspecified number of invocations of the formstd::invoke(d, g) ord.generate_random(s, g), if such an expression is well-formed for a valueN
and an objects of typestd::span<I, N>.If the effects of(1) or(3) are not equivalent to those of the corresponding fallback operation, the behavior is undefined.
The value ofN
can differ between invocations. Implementations may select smaller values for shorter ranges.
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first, last | - | the iterator-sentinel pair defining therange of elements to which random numbers are written |
r | - | therange to which random numbers are written |
g | - | uniform random bit generator |
d | - | random number distribution object |
At the time of the standardization ofstd::ranges::generate_random
, there is no random number generator or distribution in the standard library that provides agenerate_random
member function.
std::ranges::generate_random
can be more efficient when used with a user-defined random number generator that wraps an underlying vectorized API.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_generate_random | 202403L | (C++26) | std::ranges::generate_random |
#include <algorithm>#include <iomanip>#include <iostream>#include <random> int main(){std::default_random_engine eng; std::default_random_engine::result_type rs[16]{}; std::ranges::generate_random(rs, eng); std::cout<<std::left;for(int i{};auto n: rs)std::cout<<std::setw(11)<< n<<(++i%4?' ':'\n');}
Possible output:
16807 282475249 1622650073 984943658 1144108930 470211272 101027544 14578508781458777923 2007237709 823564440 11154381651784484492 74243042 114807987 1137522503
(C++20) | saves the result of a function in a range (algorithm function object)[edit] |
(C++20) | specifies that a type qualifies as a uniform random bit generator (concept)[edit] |