Namespaces
Variants

std::max_element

来自 zh.cppreference.net
 
 
算法库
受约束算法和范围算法 (C++20)
受约束算法,例如 ranges::copy, ranges::sort, ...
排序和相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在划分后的范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <algorithm>
template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );
(1) (constexpr 自 C++17 起)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt max_element( ExecutionPolicy&& policy,
                       ForwardIt first, ForwardIt last );
(2) (自 C++17 起)
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last,
                       Compare comp );
(3) (constexpr 自 C++17 起)
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt max_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 执行。
这些重载只有在以下表达式的值为 true 时才参与重载决议:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(直到 C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(自 C++20 起)

参数

first, last - 定义待检查元素范围的迭代器对
policy - 要使用的执行策略
comp - 比较函数对象(即满足比较要求),当第一个参数小于第二个参数时返回 true 。

比较函数的签名应等价于:

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

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

类型要求
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。

返回值

指向范围 [ first , last ) 中最大元素的迭代器。若范围内存在多个等价的最大元素,则返回首个该元素的迭代器。若范围为空则返回 last 。

复杂度

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

1,2) 恰好使用 operator < (C++20 前) std:: less { } (C++20 起) 进行 max(N-1,0) 次比较。
3,4) 恰好需要 max(N-1,0) 次比较函数 comp 的应用。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 是某个 标准策略 ,则调用 std::terminate 。对于其他任何 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc 。

可能的实现

max_element (1)
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
    ForwardIt largest = first;
    while (++first != last)
        if (*largest < *first)
            largest = first;
    return largest;
}
max_element (3)
template<class ForwardIt, class Compare>
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp)
{
    if (first == last)
        return last;
    ForwardIt largest = first;
    while(++first != last)
        if (comp(*largest, *first))
            largest = first;
    return largest;
}

示例

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v{3, 1, -14, 1, 5, 9, -14, 9};
    std::vector<int>::iterator result;
    result = std::max_element(v.begin(), v.end());
    std::cout << "Max element found at index "
              << std::distance(v.begin(), result)
              << " has value " << *result << '\n';
    result = std::max_element(v.begin(), v.end(), [](int a, int b)
    {
        return std::abs(a) < std::abs(b);
    });
    std::cout << "Absolute max element found at index "
              << std::distance(v.begin(), result)
              << " has value " << *result << '\n';
}

输出:

Max element found at index 5 has value 9
Absolute max element found at index 2 has value -14

缺陷报告

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

缺陷报告 适用范围 发布时行为 正确行为
LWG 212 C++98 未指定当 [ first , last ) 为空时的返回值 此情况下返回 last
LWG 2150 C++98 返回指向第一个非最小元素的迭代器 修正了返回值

参见

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