Namespaces
Variants

std::ranges::max

原文来自 zh.cppreference.net
 
 
算法库
受约束算法及范围上的算法 (C++20)
受约束算法,例如 ranges::copy、ranges::sort、……
排序及相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在已划分的范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
受约束算法
此菜单中的所有名称都属于命名空间 std::ranges
非修改序列操作
修改序列操作
划分操作
排序操作
二分搜索操作(在已排序范围上)
       
       
集合操作(在已排序范围上)
堆操作
最小/最大操作
       
       
排列操作
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
定义于头文件 <algorithm>
调用签名
template< class T, class Proj = std::identity,
          std::indirect_strict_weak_order<
              std::projected<const T*, Proj>> Comp = ranges::less >
constexpr const 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>*>
constexpr ranges::range_value_t<R>
    max( R&& r, Comp comp = {}, Proj proj = {} );
(3) (C++20 起)

返回给定投影值中较大的一个。

1) 返回 a 和 b 中较大的值。
2) 返回初始化列表 r 中最大的第一个值。
3) 返回范围 r 中最大的第一个值。

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

参数

a, b - 待比较的值
r - 待比较的数值范围
comp - 应用于投影元素的比较操作
proj - 应用于元素的投影操作

返回值

1) 根据各自的投影值,返回 a 与 b 中的较大值。若两者等价,则返回 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>
    constexpr
    const T& operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const
    {
        return std::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>*>
    constexpr
    ranges::range_value_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const
    {
        using V = ranges::range_value_t<R>;
        if constexpr (ranges::forward_range<R>)
            return
                static_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;
        }
    }
};
inline constexpr max_fn max;

注释

当某个参数是临时对象且该参数被返回时,通过引用捕获 std::ranges::max 的结果会产生悬垂引用:

int n = -1;
const int& r = std::ranges::max(n + 2, n * 2); // r 是悬垂引用

示例

#include <algorithm>
#include <iostream>
#include <string>
static_assert(std::ranges::max({0B10, 0X10, 010, 10}) == 16); // 重载 (2)
int main()
{
    namespace ranges = std::ranges;
    using namespace std::string_view_literals;
    std::cout << "1 和 9999 中的较大值: " << ranges::max(1, 9999) << '\n'
              << "'a' 和 'b' 中的较大值: '" << ranges::max('a', 'b') << "'\n"
              << "\"foo\", \"bar\" 和 \"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"

参见

返回给定值中的较小者
(算法函数对象)
返回两个元素中的较小者和较大者
(算法函数对象)
返回范围内的最大元素
(算法函数对象)
将值限制在一对边界值之间
(算法函数对象)
返回给定值中的较大者
(函数模板)