| 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::random_access_iterator I,std::sentinel_for<I> S,class Gen> requiresstd::permutable<I>&& | (1) | (since C++20) |
template<ranges::random_access_range R,class Gen> requiresstd::permutable<ranges::iterator_t<R>>&& | (2) | (since C++20) |
[first, last) such that each possible permutation of those elements has equal probability of appearance.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 shuffle randomly |
| r | - | the range of elements to shuffle randomly |
| gen | - | the random number generator |
An iterator equal tolast.
Exactly(last- first)-1 swaps.
struct shuffle_fn{template<std::random_access_iterator I,std::sentinel_for<I> S,class Gen> requiresstd::permutable<I>&&std::uniform_random_bit_generator<std::remove_reference_t<Gen>> I operator()(I first, S last, Gen&& gen)const{using diff_t=std::iter_difference_t<I>;using distr_t=std::uniform_int_distribution<diff_t>;using param_t=typename distr_t::param_type; distr_t D;constauto n{last- first};for(diff_t i{1}; i< n;++i)ranges::iter_swap(first+ i, first+ D(gen, param_t(0, i)));returnranges::next(first, last);} template<ranges::random_access_range R,class Gen> requiresstd::permutable<ranges::iterator_t<R>>&&std::uniform_random_bit_generator<std::remove_reference_t<Gen>>ranges::borrowed_iterator_t<R> operator()(R&& r, Gen&& gen)const{return(*this)(ranges::begin(r),ranges::end(r),std::forward<Gen>(gen));}}; inlineconstexpr shuffle_fn shuffle{}; |
#include <algorithm>#include <array>#include <iostream>#include <random> void print(constauto& a){for(constauto e: a)std::cout<< e<<' ';std::cout<<'\n';} int main(){std::array a{'A','B','C','D','E','F'}; print(a); std::random_device rd;std::mt19937 gen{rd()}; for(int i{}; i!=3;++i){ std::ranges::shuffle(a, gen); print(a);}}
Possible output:
A B C D E FF E A C D BE C B F A DB A E C F D
(C++20) | generates the next greater lexicographic permutation of a range of elements (algorithm function object)[edit] |
(C++20) | generates the next smaller lexicographic permutation of a range of elements (algorithm function object)[edit] |
(until C++17)(C++11) | randomly re-orders elements in a range (function template)[edit] |