Namespaces
Variants

std::ranges::minmax, std::ranges::minmax_result

来自 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 ranges::minmax_result<const T&>
    minmax( 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 ranges::minmax_result<T>
    minmax( 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::minmax_result<ranges::range_value_t<R>>
    minmax( R&& r, Comp comp = {}, Proj proj = {} );
(3) (自 C++20 起)
辅助类型
template< class T >
using minmax_result = ranges::min_max_result<T>;
(4) (自 C++20 起)

返回给定投影值中的最小值和最大值。

1) 返回对 a 和 b 中较小值和较大值的引用。
2) 返回初始化器列表 r 中的最小值和最大值。
3) 返回范围 r 中的最小值和最大值。

对于重载 (2,3),如果范围为空(由 ranges::distance(r) 确定),则行为未定义。

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

参数

a, b - 待比较的值
r - 待比较值的非空范围
comp - 应用于投影元素的比较器
proj - 应用于元素的投影器

返回值

1) { b, a } 若根据其各自的投影值, b 小于 a ;否则返回 { a, b } 。
2,3) { s, l } ,其中 s 和 l 分别是 r 中根据其投影值的最小值和最大值。若存在多个值等价于最小值和最大值,则返回最左侧的最小值和最右侧的最大值。若范围为空(通过 ranges:: distance ( r ) 判定),则行为未定义。

复杂度

1) 恰好一次比较和两次投影应用。
2,3) 最多进行 3 / 2 * ranges:: distance ( r ) 次比较以及两倍的投影函数调用。

可能的实现

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>
    requires std::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)};
    }
};
inline constexpr minmax_fn minmax;

注释

对于重载 (1) ,如果其中一个参数是临时对象,则返回的引用将在包含对 minmax 调用的完整表达式结束时成为悬垂引用:

int n = 1;
auto p = std::ranges::minmax(n, n + 1);
int m = p.min; // 正确
int x = p.max; // 未定义行为
// 注意结构化绑定存在相同问题
auto [mm, xx] = std::ranges::minmax(n, n + 1);
xx; // 未定义行为

示例

#include <algorithm>
#include <array>
#include <iostream>
#include <random>
int main()
{
    namespace ranges = std::ranges;
    constexpr std::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: 悬垂引用:bounds.min 和 bounds.max 的类型为 `const int&`
    const int x1 = distribution(generator);
    const int x2 = distribution(generator);
    auto bounds = ranges::minmax(x1, x2); // 正确:获取了左值 x1 和 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';
}

可能的输出:

v[3:9]: 1 5 9 2 6 5 
smallest: 1, largest: 9

参见

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