Namespaces
Variants

std:: minmax_element

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_element
(C++11)

Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
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 - 比较函数对象(即满足比较要求),若第一参数小于第二参数则返回 true

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

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

虽然签名不需要包含 const & ,但函数不得修改传递的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论值类别为何(因而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起) )。
类型 Type1 Type2 必须使得 ForwardIt 类型的对象在解引用后能隐式转换到这两个类型。

类型要求
-
ForwardIt 必须满足 向前迭代器 的要求。

返回值

一个以指向最小元素的迭代器作为第一个元素,指向最大元素的迭代器作为第二个元素的配对。若范围为空则返回 std:: make_pair ( first, first ) 。若有多个元素等价于最小元素,则返回指向首个此类元素的迭代器。若有多个元素等价于最大元素,则返回指向最后一个此类元素的迭代器。

复杂度

给定 N std:: distance ( first, last )

1,2) 最多进行 max(⌊
3
2
(N-1)⌋,0)
次比较,使用 operator < (C++20 前) std:: less { } (C++20 起)
3,4) 最多 max(⌊
3
2
(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';
}

输出:

min = 1, max = 9

参见

返回范围内的最小元素
(函数模板)
返回范围内的最大元素
(函数模板)
返回范围内的最小和最大元素
(算法函数对象)