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 <algorithm> | ||
Call signature | ||
template<std::input_or_output_iterator O,std::copy_constructible F> requiresstd::invocable<F&>&&std::indirectly_writable<O,std::invoke_result_t<F&>> | (since C++20) | |
Assigns the result ofsuccessive invocations of the function objectgen to each element in the range[
first,
first+ n)
, if0< n. Does nothing otherwise.
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first | - | the beginning of the range of elements to modify |
n | - | number of elements to modify |
gen | - | the generator function object. |
Iterator one past the last element assigned if0< count,first otherwise.
Exactlyn invocations ofgen() and assignments.
struct generate_n_fn{template<std::input_or_output_iterator O,std::copy_constructible F> requiresstd::invocable<F&>&&std::indirectly_writable<O,std::invoke_result_t<F&>>constexpr O operator()(O first,std::iter_difference_t<O> n, F gen)const{for(; n-->0;*first=std::invoke(gen),++first){}return first;}}; inlineconstexpr generate_n_fn generate_n{}; |
#include <algorithm>#include <array>#include <iostream>#include <random>#include <string_view> auto dice(){staticstd::uniform_int_distribution<int> distr{1,6};staticstd::random_device engine;staticstd::mt19937 noise{engine()};return distr(noise);} void print(constauto& v,std::string_view comment){for(int i: v)std::cout<< i<<' ';std::cout<<'('<< comment<<")\n";} int main(){std::array<int,8> v; std::ranges::generate_n(v.begin(), v.size(), dice); print(v,"dice"); std::ranges::generate_n(v.begin(), v.size(),[n{0}] mutable{return n++;});// same effect as std::iota(v.begin(), v.end(), 0); print(v,"iota");}
Possible output:
5 5 2 2 6 6 3 5 (dice)0 1 2 3 4 5 6 7 (iota)
(C++20) | saves the result of a function in a range (algorithm function object)[edit] |
(C++26) | fills a range with random numbers from a uniform random bit generator (algorithm function object)[edit] |
(C++20) | assigns a range of elements a certain value (algorithm function object)[edit] |
(C++20) | assigns a value to a number of elements (algorithm function object)[edit] |
(C++20) | applies a function to a range of elements (algorithm function object)[edit] |
assigns the results of successive function calls to N elements in a range (function template)[edit] |