std::minmax

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

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <algorithm> 定义
template< class T >
std::pair<const T&, const T&> minmax( const T& a, const T& b );
(1) (C++11 起)
(C++14 起为 constexpr)
template< class T, class Compare >

std::pair<const T&, const T&> minmax( const T& a, const T& b,

                                      Compare comp );
(2) (C++11 起)
(C++14 起为 constexpr)
template< class T >
std::pair<T, T> minmax( std::initializer_list<T> ilist );
(3) (C++11 起)
(C++14 起为 constexpr)
template< class T, class Compare >

std::pair<T, T> minmax( std::initializer_list<T> ilist,

                        Compare comp );
(4) (C++11 起)
(C++14 起为 constexpr)

返回给定值的最小和最大者。

1,2) 返回到 ab 较小和较大者的引用。
1) 使用 operator< 来比较两个值。
如果 T可小于比较 (LessThanComparable) ,那么行为未定义。
2) 使用比较函数 comp 来比较两个值。
3,4) 返回初始化器列表 ilist 中值的最小和最大者。
如果 ilist.size() 为零,或者 T可复制构造 (CopyConstructible) ,那么行为未定义。
3) 使用 operator< 来比较这些值。
如果 T可小于比较 (LessThanComparable) ,那么行为未定义。
4) 使用比较函数 comp 来比较这些值。

参数

a, b - 要比较的值
ilist - 拥有要比较的值的初始化器列表
cmp - 比较函数对象(即满足比较 (Compare) 要求的对象),如果a 小于 b,则返回 ​true

比较函数的签名应等价于如下:

 bool cmp(const Type1 &a, const Type2 &b);

虽然签名不必有 const&,函数也不能修改传递给它的对象,而且必须能够接受(可有 const 限定的)类型 Type1Type2 的所有值,与值类别无关(从而不允许 Type1 &,也不允许 Type1,除非 Type1 的移动等价于复制 (C++11 起))。
类型 Type1Type2 必须使得 T 类型的对象能隐式转换到这两个类型。 ​

返回值

1,2) 如果 a < b 或者 a 等价于 b,那么返回 std::pair<const T&, const T&>(a, b) 的结果。如果 b < a,那么返回 std::pair<const T&, const T&>(b, a) 的结果。
3,4)ilist 中最小元素为第一元素,最大元素为第二元素的 pair。如果有多个等价于最小者的值,那么返回最左侧的这种值。如果有多个等价于最大者的值,那么返回最右侧的这种值。

复杂度

1) 应用一次 operator< 进行比较。
2) 应用一次比较函数 comp
3,4) 给定 Nilist.size()
3) 最多应用
3N
2
operator< 进行比较。
4) 最多应用
3N
2
次比较函数 comp

可能的实现

minmax (1)
template<class T>
constexpr std::pair<const T&, const T&> minmax(const T& a, const T& b)
{
    return (b < a) ? std::pair<const T&, const T&>(b, a)
                   : std::pair<const T&, const T&>(a, b);
}
minmax (2)
template<class T, class Compare>
constexpr std::pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp)
{
    return comp(b, a) ? std::pair<const T&, const T&>(b, a)
                      : std::pair<const T&, const T&>(a, b);
}
minmax (3)
template<class T>
constexpr std::pair<T, T> minmax(std::initializer_list<T> ilist)
{
    auto p = std::minmax_element(ilist.begin(), ilist.end());
    return std::pair(*p.first, *p.second);
}
minmax (4)
template<class T, class Compare>
constexpr std::pair<T, T> minmax(std::initializer_list<T> ilist, Compare comp)
{
    auto p = std::minmax_element(ilist.begin(), ilist.end(), comp);
    return std::pair(*p.first, *p.second);
}

注解

对于重载 (1,2),如果实参之一是临时量,那么返回的引用在包含对 minmax 调用的完整表达式结尾变为悬垂引用:

int n = 1;
auto p = std::minmax(n, n + 1);
int m = p.first; // ok
int x = p.second; // 未定义行为
 
// 注意结构化绑定有同样的问题
auto [mm, xx] = std::minmax(n, n + 1);
xx; // 未定义行为

示例

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9, 2, 6};
    std::srand(std::time(0));
    std::pair<int, int> bounds = std::minmax(std::rand() % v.size(),
                                             std::rand() % v.size());
 
    std::cout << "v[" << bounds.first << "," << bounds.second << "]:";
    for (int i = bounds.first; i < bounds.second; ++i)
        std::cout << v[i] << ' ';
    std::cout << '\n';
}

可能的输出:

v[2,7]:4 1 5 9 2

参阅

返回各给定值中的较小者
(函数模板)
返回各给定值中的较大者
(函数模板)
返回范围内的最小元素和最大元素
(函数模板)
返回两个元素的较小和较大者
(niebloid)