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<class T,class Proj=std::identity, std::indirect_strict_weak_order< | (1) | (since C++20) |
template<std::copyable T,class Proj=std::identity, std::indirect_strict_weak_order< | (2) | (since C++20) |
template<ranges::input_range R,class Proj=std::identity, std::indirect_strict_weak_order< | (3) | (since C++20) |
Helper types | ||
template<class T> using minmax_result=ranges::min_max_result<T>; | (4) | (since C++20) |
Returns the smallest and the greatest of the given projected values.
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
a, b | - | the values to compare |
r | - | a non-empty range of values to compare |
comp | - | comparison to apply to the projected elements |
proj | - | projection to apply to the elements |
s
andl
are respectively the smallest and largest values inr, according to their projected value. If several values are equivalent to the smallest and largest, returns the leftmost smallest value, and the rightmost largest value. If the range is empty (as determined byranges::distance(r)), the behavior is undefined.struct minmax_fn{template<class T,class Proj=std::identity,std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp=ranges::less>constexpr ranges::minmax_result<const T&> operator()(const T& a,const T& b, Comp comp={}, Proj proj={})const{if(std::invoke(comp,std::invoke(proj, b),std::invoke(proj, a)))return{b, a}; return{a, b};} template<std::copyable T,class Proj=std::identity,std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp=ranges::less>constexpr ranges::minmax_result<T> operator()(std::initializer_list<T> r, Comp comp={}, Proj proj={})const{auto result=ranges::minmax_element(r,std::ref(comp),std::ref(proj));return{*result.min,*result.max};} template<ranges::input_range R,class Proj=std::identity,std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R>, Proj>> Comp=ranges::less> requiresstd::indirectly_copyable_storable<ranges::iterator_t<R>,ranges::range_value_t<R>*>constexpr ranges::minmax_result<ranges::range_value_t<R>> operator()(R&& r, Comp comp={}, Proj proj={})const{auto result=ranges::minmax_element(r,std::ref(comp),std::ref(proj));return{std::move(*result.min), std::move(*result.max)};}}; inlineconstexpr minmax_fn minmax; |
For overload(1), if one of the parameters is a temporary, the reference returned becomes a dangling reference at the end of the full expression that contains the call tominmax
:
int n=1;auto p= std::ranges::minmax(n, n+1);int m= p.min;// okint x= p.max;// undefined behavior // Note that structured bindings have the same issueauto[mm, xx]= std::ranges::minmax(n, n+1);xx;// undefined behavior
#include <algorithm>#include <array>#include <iostream>#include <random> int main(){namespace ranges= std::ranges; constexprstd::array v{3,1,4,1,5,9,2,6,5}; std::random_device rd;std::mt19937_64 generator(rd());std::uniform_int_distribution<> distribution(0,ranges::distance(v));// [0..9] // auto bounds = ranges::minmax(distribution(generator), distribution(generator));// UB: dangling references: bounds.min and bounds.max have the type `const int&`. constint x1= distribution(generator);constint x2= distribution(generator);auto bounds= ranges::minmax(x1, x2);// OK: got references to lvalues x1 and x2 std::cout<<"v["<< bounds.min<<":"<< bounds.max<<"]: ";for(int i= bounds.min; i< bounds.max;++i)std::cout<< v[i]<<' ';std::cout<<'\n'; auto[min, max]= ranges::minmax(v);std::cout<<"smallest: "<< min<<", "<<"largest: "<< max<<'\n';}
Possible output:
v[3:9]: 1 5 9 2 6 5 smallest: 1, largest: 9
(C++20) | returns the smaller of the given values (algorithm function object)[edit] |
(C++20) | returns the greater of the given values (algorithm function object)[edit] |
(C++20) | returns the smallest and the largest elements in a range (algorithm function object)[edit] |
(C++20) | clamps a value between a pair of boundary values (algorithm function object)[edit] |
(C++11) | returns the smaller and larger of two elements (function template)[edit] |