std::ranges::minmax, std::ranges::minmax_result - cppreference.com

Returns the smallest and the greatest of the given projected values.

1) Returns references to the smaller and the greater of a and b.

2) Returns the smallest and the greatest of the values in the initializer list r.

3) Returns the smallest and the greatest of the values in the range r.

For overloads (2,3), the behavior is undefined if the range is empty (as determined by ranges::distance(r)).

The function-like entities described on this page are

algorithm function objects

(informally known as niebloids), that is:

1){b,a} if, according to their respective projected value, b is smaller than a; otherwise it returns {a,b}.

2,3){s,l}, where s and l are respectively the smallest and largest values in r, 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.

1) Exactly one comparison and two applications of the projection.

2,3) At most 3/2*ranges::distance(r) comparisons and twice as many applications of the projection.

structminmax_fn{template<classT,classProj=std::identity,std::indirect_strict_weak_order<std::projected<constT*,Proj>>Comp=ranges::less>constexprranges::minmax_result<constT&>operator()(constT&a,constT&b,Compcomp={},Projproj={})const{if(std::invoke(comp,std::invoke(proj,b),std::invoke(proj,a)))return{b,a};return{a,b};}template<std::copyableT,classProj=std::identity,std::indirect_strict_weak_order<std::projected<constT*,Proj>>Comp=ranges::less>constexprranges::minmax_result<T>operator()(std::initializer_list<T>r,Compcomp={},Projproj={})const{autoresult=ranges::minmax_element(r,std::ref(comp),std::ref(proj));return{*result.min,*result.max};}template<ranges::input_rangeR,classProj=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>*>constexprranges::minmax_result<ranges::range_value_t<R>>operator()(R&&r,Compcomp={},Projproj={})const{autofirst=ranges::begin(r);constautolast=ranges::end(r);automin=static_cast<range_value_t<R>>(*first),max=min;while(++first!=last){autox=static_cast<range_value_t<R>>(*first);if(++first==last){if(std::invoke(comp,std::invoke(proj,x),std::invoke(proj,min)))min=x;elseif(!std::invoke(comp,std::invoke(proj,x),std::invoke(proj,max)))max=x;break;}if(std::invoke(comp,std::invoke(proj,*first),std::invoke(proj,x))){if(std::invoke(comp,std::invoke(proj,*first),std::invoke(proj,min)))min=*first;if(!std::invoke(comp,std::invoke(proj,x),std::invoke(proj,max)))max=x;}else{if(std::invoke(comp,std::invoke(proj,x),std::invoke(proj,min)))min=x;if(!std::invoke(comp,std::invoke(proj,*first),std::invoke(proj,max)))max=*first;}}return{min,max};}};inlineconstexprminmax_fnminmax;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 to minmax:

intn=1;autop=std::ranges::minmax(n,n+1);intm=p.min;// okintx=p.max;// undefined behavior// Note that structured bindings have the same issueauto[mm,xx]=std::ranges::minmax(n,n+1);xx;// undefined behaviorRun this code

#include<algorithm>#include<array>#include<iostream>#include<random>intmain(){namespaceranges=std::ranges;constexprstd::arrayv{3,1,4,1,5,9,2,6,5};std::random_devicerd;std::mt19937_64generator(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&`.constintx1=distribution(generator);constintx2=distribution(generator);autobounds=ranges::minmax(x1,x2);// OK: got references to lvalues x1 and x2std::cout<<"v["<<bounds.min<<":"<<bounds.max<<"]: ";for(inti=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