std::max_element

来自cppreference.com
< cpp‎ | algorithm

 
 
算法库
功能
原文:
Functions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
修改序列操作
原文:
Non-modifying sequence operations
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal
修改序列操作
原文:
Modifying sequence operations
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
分区操作
原文:
Partitioning operations
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
is_partitioned(C++11)
partition
partition_copy(C++11)
排序操作(排序的区间)
原文:
Sorting operations (on sorted ranges)
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
is_sorted(C++11)
is_sorted_until(C++11)
sort
二进制搜索操作(排序的区间)
原文:
Binary search operations (on sorted ranges)
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
设置操作(排序的区间)
原文:
Set operations (on sorted ranges)
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
堆的操作
原文:
Heap operations
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
最小/最大操作
原文:
Minimum/maximum operations
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
max
max_element
min
min_element
minmax(C++11)
minmax_element(C++11)
数字操作
原文:
Numeric operations
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
C库
原文:
C library
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
 
在头文件 <algorithm> 中定义
template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );
(1)
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );
(2)

Finds the greatest element in the range [first, last). The first version uses operator< to compare the values, the second version uses the given comparison function comp.

目录

[编辑] 参数

first, last -
前向迭代器定义的范围检查
原文:
forward iterators defining the range to examine
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
cmp - comparison function which returns ​true if if *a is less than *b.

The signature of the comparison function should be equivalent to the following:

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

The signature does not need to have const &, but the function must not modify the objects passed to it.
类型 Type1Type2 必须能使类型 ForwardIt 的对象能够 be dereferenced and then 隐式转换 to both of them。 ​

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

[编辑] 返回值

Iterator to the greatest element in the range [first, last). If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element. Returns last if the range is empty.

[编辑] 复杂度

究竟max(N-1,0)比较,N = std::distance(first, last).
原文:
Exactly max(N-1,0) comparisons, where N = std::distance(first, last).
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 可能的实现

版本一
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last)
{
    if (first == last) {
        return last;
    }
    ForwardIt largest = first;
    ++first;
    for (; first != last; ++first) {
        if (*largest < *first) {
            largest = first;
        }
    }
    return largest;
}
版本二
template<class ForwardIt, class Compare>
ForwardIt max_element(ForwardIt first, ForwardIt last, 
                            Compare comp)
{
    if (first == last) {
        return last;
    }
    ForwardIt largest = first;
    ++first;
    for (; first != last; ++first) {
        if (comp(*largest, *first)) {
            largest = first;
        }
    }
    return largest;
}

[编辑] 示例

#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
 
static bool abs_compare(int a, int b)
{
    return (std::abs(a) < std::abs(b));
}
 
int main()
{
    std::vector<int> v{ 3, 1, -14, 1, 5, 9 }; 
    std::vector<int>::iterator result;
 
    result = std::max_element(v.begin(), v.end());
    std::cout << "max element at: " << std::distance(v.begin(), result) << '\n';
 
    result = std::max_element(v.begin(), v.end(), abs_compare);
    std::cout << "max element (absolute) at: " << std::distance(v.begin(), result);
}

输出:

max element at: 5
max element (absolute) at: 2

[编辑] 另请参阅

返回区间内的最小元素
(函数模板) [edit]
(C++11)
返回区间内的最小元素和最大元素
(函数模板) [edit]
返回两个元素中的较大者
(函数模板) [edit]