Namespaces
Variants

std::ranges:: max_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
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: forward_iterator I, std:: sentinel_for < I > S, class Proj = std:: identity ,

std:: indirect_strict_weak_order < std :: projected < I, Proj >> Comp = ranges:: less >
constexpr I

max_element ( I first, S last, Comp comp = { } , Proj proj = { } ) ;
(1) (C++20 起)
template < ranges:: forward_range R, class Proj = std:: identity ,

std:: indirect_strict_weak_order <
std :: projected < ranges:: iterator_t < R > , Proj >> Comp = ranges:: less >
constexpr ranges:: borrowed_iterator_t < R >

max_element ( R && r, Comp comp = { } , Proj proj = { } ) ;
(2) (C++20 起)
1) 在区间 [ first , last ) 中查找最大元素。
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,并使用 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待检查元素 范围 的迭代器-哨位对
r - 待检查的 range
comp - 应用于投影元素的比较器
proj - 应用于元素的投影器

返回值

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

复杂度

恰好需要 max(N - 1, 0) 次比较,其中 N = ranges:: distance ( first, last )

可能的实现

struct max_element_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_strict_weak_order<std::projected<I, Proj>> Comp = ranges::less>
    constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
    {
        if (first == last)
            return last;
        auto largest = first;
        while (++first != last)
            if (std::invoke(comp, std::invoke(proj, *largest), std::invoke(proj, *first)))
                largest = first;
        return largest;
    }
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Comp comp = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
    }
};
inline constexpr max_element_fn max_element;

示例

#include <algorithm>
#include <cmath>
#include <iostream>
int main()
{
    namespace ranges = std::ranges;
    const auto v = {3, 1, -14, 1, 5, 9, -14, 9};
    auto result = ranges::max_element(v.begin(), v.end());
    std::cout << "Max element at pos " << ranges::distance(v.begin(), result) << '\n';
    auto abs_compare = [](int a, int b) { return std::abs(a) < std::abs(b); };
    result = ranges::max_element(v, abs_compare);
    std::cout << "Absolute max element at pos "
              << ranges::distance(v.begin(), result) << '\n';
}

输出:

Max element at pos 5
Absolute max element at pos 2

参见

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