std::minmax
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<algorithm>
定义
|
||
(1) | ||
template< class T >
std::pair<const T&,const T&> minmax( const T& a, const T& b ); |
(C++11 起) (C++14 前) |
|
template< class T >
constexpr std::pair<const T&,const T&> minmax( const T& a, const T& b ); |
(C++14 起) | |
(2) | ||
template< class T, class Compare >
std::pair<const T&,const T&> minmax( const T& a, const T& b, |
(C++11 起) (C++14 前) |
|
template< class T, class Compare >
constexpr std::pair<const T&,const T&> minmax( const T& a, const T& b, |
(C++14 起) | |
(3) | ||
template< class T >
std::pair<T,T> minmax( std::initializer_list<T> ilist); |
(C++11 起) (C++14 前) |
|
template< class T >
constexpr std::pair<T,T> minmax( std::initializer_list<T> ilist); |
(C++14 起) | |
(4) | ||
template< class T, class Compare >
std::pair<T,T> minmax( std::initializer_list<T> ilist, Compare comp ); |
(C++11 起) (C++14 前) |
|
template< class T, class Compare >
constexpr std::pair<T,T> minmax( std::initializer_list<T> ilist, Compare comp ); |
(C++14 起) | |
返回给定值的最小和最大者。
a
的 b
较小和较大者的引用。
ilist
中值的最小和最大者。
(1,3) 版本用 operator<
比较值,而 (2,4) 版本用给定的比较函数 comp
。
参数
a, b | - | 要比较的值 |
ilist | - | 含要比较的值的 initializer_list |
comp | - | 比较函数对象(即满足比较 (Compare)
要求的对象),如果首个参数小于 第二个,那么返回 true。 比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const &,函数也不能修改传递给它的对象,而且必须接受(可有 const
限定的)类型 |
类型要求 | ||
- 为使用重载 (1,3), T 必须符合可小于比较 (LessThanComparable)
的要求。
|
||
- 为使用重载 (3,4), T 必须符合可复制构造
(CopyConstructible)
的要求。
|
返回值
a<b
或若 a
等价于
b
则返回 std::pair<const T&, const T&>(a, b) 的结果。若 b<a
则返回 std::pair<const T&, const T&>(b,
a) 的结果。ilist
中最小元素为第一元素,最大元素为第二元素的 pair
。若有数个元素等价于最小者,则返回最左的这种元素。若有数个元素等价于最大者,则返回最右的这种元素。复杂度
ilist.size() * 3 / 2
次比较可能的实现
版本一 |
---|
版本二 |
版本三 |
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); } |
版本四 |
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 <iostream> #include <vector> #include <cstdlib> #include <ctime> 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++11)
|
返回范围内的最小元素和最大元素 (函数模板) |
(C++20)
|
返回两个元素的较小和较大者 (niebloid) |