std::ranges::max

来自cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
算法库
受约束算法及范围上的算法(C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
受约束算法
本菜单中的所有名字均属于命名空间 std::ranges
不修改序列的操作
修改序列的操作
划分操作
排序操作
二分搜索操作(在有序范围上)
       
       
集合操作(在有序范围上)
堆操作
最小/最大操作
max
       
       
排列操作
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
在标头 <algorithm> 定义
调用签名
template<class T, class Proj =std::identity,

          std::indirect_strict_weak_order<
              std::projected<const T*, Proj>> Comp =ranges::less>
constexprconst T&

    max(const T& a, const T& b, Comp comp ={}, Proj proj ={});
(1) (C++20 起)
template<std::copyable T, class Proj =std::identity,

          std::indirect_strict_weak_order<
              std::projected<const T*, Proj>> Comp =ranges::less>
constexpr T

    max(std::initializer_list<T> r, Comp comp ={}, Proj proj ={});
(2) (C++20 起)
template<ranges::input_range R, class Proj =std::identity,

          std::indirect_strict_weak_order<
              std::projected<ranges::iterator_t<R>, Proj>> Comp =ranges::less>
requires std::indirectly_copyable_storable<ranges::iterator_t<R>,
                                           ranges::range_value_t<R>*>
constexprranges::range_value_t<R>

    max( R&& r, Comp comp ={}, Proj proj ={});
(3) (C++20 起)

返回给定的投影值的较大者。

1) 返回 ab 的较大者。
2) 返回 initializer_list r 中的首个最大值。
3) 返回范围 r 中的首个最大值。

此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:

目录

[编辑]参数

a, b - 要比较的值
r - 要比较的值的范围
comp - 应用到投影后元素的比较
proj - 应用到元素的投影

[编辑]返回值

1)ab 的较大值,按照其各自的投影值。若它们等价则返回 a
2-3)r 中按照投影的最大值。若数个值等价于最大值,则返回最左者。若范围为空(由 ranges::distance(r) 确定)则行为未定义。

[编辑]复杂度

1) 准确比较一次。
2,3) 准确比较 ranges::distance(r)-1 次。

[编辑]可能的实现

struct max_fn {template<class T, class Proj =std::identity, std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp =ranges::less>constexprconst T& operator()(const T& a, const T& b, Comp comp ={}, Proj proj ={})const{returnstd::invoke(comp, std::invoke(proj, a), std::invoke(proj, b))? b : a;}   template<std::copyable T, class Proj =std::identity, std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp =ranges::less>constexpr T operator()(std::initializer_list<T> r, Comp comp ={}, Proj proj ={})const{return*ranges::max_element(r, std::ref(comp), std::ref(proj));}   template<ranges::input_range R, class Proj =std::identity, std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R>, Proj>> Comp =ranges::less> requires std::indirectly_copyable_storable<ranges::iterator_t<R>, ranges::range_value_t<R>*>constexprranges::range_value_t<R> operator()(R&& r, Comp comp ={}, Proj proj ={})const{using V =ranges::range_value_t<R>;ifconstexpr(ranges::forward_range<R>)returnstatic_cast<V>(*ranges::max_element(r, std::ref(comp), std::ref(proj)));else{auto i =ranges::begin(r);auto s =ranges::end(r); V m(*i);while(++i != s)if(std::invoke(comp, std::invoke(proj, m), std::invoke(proj, *i))) m =*i;return m;}}};   inlineconstexpr max_fn max;

[编辑]注解

如果参数之一是临时量,而该参数被返回,那么以引用捕获 std::ranges::max 的结果会产生一个悬垂引用:

int n =1;constint& r = std::ranges::max(n -1, n +1);// r 悬垂

[编辑]示例

#include <algorithm>#include <iostream>#include <string>   static_assert(std::ranges::max({0B10, 0X10, 010, 10})==16);// 重载 (2)   int main(){namespace ranges = std::ranges;usingnamespace std::string_view_literals;   std::cout<<"larger of 1 and 9999: "<< ranges::max(1, 9999)<<'\n'<<"larger of 'a', and 'b': '"<< ranges::max('a', 'b')<<"'\n"<<"longest of \"foo\", \"bar\", and \"hello\": \""<< ranges::max({"foo"sv, "bar"sv, "hello"sv}, {}, &std::string_view::size)<<"\"\n";}

输出:

larger of 1 and 9999: 9999 larger of 'a', and 'b': 'b' longest of "foo", "bar", and "hello": "hello"

[编辑]参阅

返回给定值中较小者
(算法函数对象)[编辑]
返回两个元素间的较小者和较大者
(算法函数对象)[编辑]
返回范围中最大元
(算法函数对象)[编辑]
在一对边界值下夹逼一个值
(算法函数对象)[编辑]
返回给定值中较大者
(函数模板)[编辑]
close