std::minmax
来自cppreference.com
<tbody>
</tbody>
| 在标头 <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 中值的最小和最大者。3) 使用
operator< 来比较这些值。 如果
T 不可小于比较 (LessThanComparable) ,那么行为未定义。4) 使用比较函数
comp 来比较这些值。参数
| a, b | - | 要比较的值 |
| ilist | - | 拥有要比较的值的初始化器列表 |
| cmp | - | 比较函数对象(即满足比较 (Compare) 要求的对象),如果a 小于 b,则返回 true。比较函数的签名应等价于如下:
虽然签名不必有 |
返回值
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) 给定 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; // 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
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2239 | C++11 | 重载 (2,4) 中 T 需要可小于比较 (LessThanComparable)
|
不需要 |
参阅
| 返回给定值中较小者 (函数模板) | |
| 返回给定值中较大者 (函数模板) | |
(C++11) |
返回范围中的最小元和最大元 (函数模板) |
(C++20) |
返回两个元素间的较小者和较大者 (算法函数对象) |