template< class ForwardIt >
std::pair<ForwardIt, ForwardIt>
minmax_element( ForwardIt first, ForwardIt last );
|
(1) |
(C++11 起) (C++17 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt >
std::pair<ForwardIt, ForwardIt>
minmax_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );
|
(2) |
(C++17 起) |
template< class ForwardIt, class Compare >
std::pair<ForwardIt, ForwardIt>
minmax_element( ForwardIt first, ForwardIt last, Compare comp );
|
(3) |
(C++11 起) (C++17 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class Compare >
std::pair<ForwardIt, ForwardIt>
minmax_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, Compare comp );
|
(4) |
(C++17 起) |
| | |
寻找范围 [first, last) 中最小和最大的元素。
1) 用 operator<(C++20 前)std::less{}(C++20 起) 比较元素。
3) 用比较函数 comp 比较元素。
2,4) 同 (1,3),但按照 policy 执行。
这些重载只有在满足以下所有条件时才会参与重载决议:
|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 是 true。
|
(C++20 前) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 是 true。
|
(C++20 起) |
参数
| first, last
|
-
|
要检验的元素范围的迭代器对
|
| policy
|
-
|
所用的执行策略
|
| cmp
|
-
|
比较函数对象(即满足比较 (Compare) 要求的对象),如果首个实参小于 第二个,则返回 true。
比较函数的签名应等价于如下:
bool cmp(const Type1 &a, const Type2 &b);
虽然签名不必有 const&,函数也不能修改传递给它的对象,而且必须能够接受(可有 const 限定的)类型 Type1 与 Type2 的所有值,与值类别无关(从而不允许 Type1 &,也不允许 Type1,除非 Type1 的移动等价于复制(C++11 起))。
类型 Type1 与 Type2 必须使得 ForwardIt 类型的对象能在解引用后隐式转换到这两个类型。
|
| 类型要求
|
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
|
返回值
以指向最小元素的迭代器为第一元素,以指向最大元素的迭代器为第二元素的 pair。范围为空时返回 std::make_pair(first, first)。如果多个元素等价于最小元素,那么返回指向首个这种元素的迭代器。如果多个元素等价于最大元素,那么返回指向最后一个这种元素的迭代器。
复杂度
给定 \(\scriptsize N\)N 为 std::distance(first, last):
1,2) 最多应用
\(\scriptsize \max(\left\lfloor \frac{3}{2}(N-1) \right\rfloor, 0)\)max(⌊(N-1)⌋,0) 次
operator<(C++20 前)std::less{}(C++20 起) 进行比较。
3,4) 最多应用
\(\scriptsize \max(\left\lfloor \frac{3}{2}(N-1) \right\rfloor, 0)\)max(⌊(N-1)⌋,0) 次比较函数
comp。
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy 是标准策略之一,那么调用 std::terminate。对于任何其他 ExecutionPolicy,行为由实现定义。
- 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| minmax_element
|
template<class ForwardIt>
std::pair<ForwardIt, ForwardIt>
minmax_element(ForwardIt first, ForwardIt last)
{
using value_type = typename std::iterator_traits<ForwardIt>::value_type;
return std::minmax_element(first, last, std::less<value_type>());
}
|
| minmax_element
|
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt>
minmax_element(ForwardIt first, ForwardIt last, Compare comp)
{
auto min = first, max = first;
if (first == last || ++first == last)
return {min, max};
if (comp(*first, *min))
min = first;
else
max = first;
while (++first != last)
{
auto i = first;
if (++first == last)
{
if (comp(*i, *min))
min = i;
else if (!(comp(*i, *max)))
max = i;
break;
}
else
{
if (comp(*first, *i))
{
if (comp(*first, *min))
min = first;
if (!(comp(*i, *max)))
max = i;
}
else
{
if (comp(*i, *min))
min = i;
if (!(comp(*first, *max)))
max = first;
}
}
}
return {min, max};
}
|
注解
此算法不仅在效率上与 std::make_pair(std::min_element(), std::max_element()) 不同,而且此算法寻找最后 的最大元素,而 std::max_element 寻找首个 最大元素。
示例
#include <algorithm>
#include <iostream>
int main()
{
const auto v = {3, 9, 1, 4, 2, 5, 9};
const auto [min, max] = std::minmax_element(begin(v), end(v));
std::cout << "min = " << *min << ", max = " << *max << '\n';
}
输出:
参阅
|
|
返回范围中最小元 (函数模板) |
|
|
返回范围中最大元 (函数模板) |
|
|
返回范围中的最小元和最大元 (算法函数对象) |