Namespaces
Variants

std:: minmax

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
minmax
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <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) 返回 a b 中较小值和较大值的引用。
1) 使用 operator < 来比较数值。
如果 T 不满足 LessThanComparable 要求,则行为未定义。
2) 使用比较函数 comp 来比较数值。
3,4) 返回初始化列表 ilist 中的最小值和最大值。
如果 ilist. size ( ) 为零,或 T 不满足 CopyConstructible 要求,则行为未定义。
3) 使用 operator < 来比较数值。
如果 T 不满足 LessThanComparable 要求,则行为未定义。
4) 使用比较函数 comp 来比较数值。

目录

参数

a, b - 要比较的值
ilist - 包含待比较值的初始化列表
comp - 比较函数对象(即满足 Compare 要求的对象),当第一个参数 小于 第二个参数时返回 true

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

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

虽然签名不需要包含 const & ,但函数不得修改传递的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许使用 Type1 & ,也不允许使用 Type1 ,除非对于 Type1 移动操作等价于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 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 中最小值为首元素、最大值为次元素构成的元素对。若存在多个最小等价元素,则返回最左侧的最小元素。若存在多个最大等价元素,则返回最右侧的最大元素。

复杂度

1) 仅使用一次 operator < 进行比较。
2) 仅进行一次比较函数 comp 的应用。
3,4) 给定 N ilist. 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; // 正确
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

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 适用范围 发布时的行为 正确行为
LWG 2239 C++11 重载版本 ( 2,4 ) 要求 T 满足 LessThanComparable 要求 无需此要求

参见

返回给定值中的较小者
(函数模板)
返回给定值中的较大者
(函数模板)
返回范围中的最小和最大元素
(函数模板)
返回两个元素中的较小者和较大者
(算法函数对象)