跳到主要内容

std::max_element() 算法

template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );

// (2)
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );

// (3)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt max_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );

// (4)
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt max_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, Compare comp );

查找范围 [first; last) 中的最大元素。

  • (1) 元素使用 operator< 进行比较。

  • (2) 元素使用给定的二元比较函数 comp 进行比较。

参数

first
last

要查找最大元素的范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受类型为 (可能是 const) TypeType2 的所有值,无论值类别如何 (因此不允许 Type1&除非 Type1 的移动等同于复制,否则也不允许 Type1 (自 C++11 起))
  • 类型 Type1Type2 必须使得 ForwardIt 类型的对象可以隐式转换为它们两者。

类型要求

RandomItLegacyRandomAccessIterator

返回值

指向范围 [first; last) 中最大元素的迭代器。
如果范围中有多个元素与最大元素等价,则返回指向第一个此类元素的迭代器。

如果范围为空,则返回 last

复杂度

给定 Nstd::distance(first, last)

精确地 max(N - 1, 0) 次比较。

异常

带有模板参数 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;
++first;

for (; first != last; ++first)
if (*largest < *first)
largest = first;

return largest;
}
max_element(2)
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;
}

示例

Main.cpp
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

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 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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::max_element() 算法

template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );

// (2)
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );

// (3)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt max_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );

// (4)
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt max_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, Compare comp );

查找范围 [first; last) 中的最大元素。

  • (1) 元素使用 operator< 进行比较。

  • (2) 元素使用给定的二元比较函数 comp 进行比较。

参数

first
last

要查找最大元素的范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受类型为 (可能是 const) TypeType2 的所有值,无论值类别如何 (因此不允许 Type1&除非 Type1 的移动等同于复制,否则也不允许 Type1 (自 C++11 起))
  • 类型 Type1Type2 必须使得 ForwardIt 类型的对象可以隐式转换为它们两者。

类型要求

RandomItLegacyRandomAccessIterator

返回值

指向范围 [first; last) 中最大元素的迭代器。
如果范围中有多个元素与最大元素等价,则返回指向第一个此类元素的迭代器。

如果范围为空,则返回 last

复杂度

给定 Nstd::distance(first, last)

精确地 max(N - 1, 0) 次比较。

异常

带有模板参数 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;
++first;

for (; first != last; ++first)
if (*largest < *first)
largest = first;

return largest;
}
max_element(2)
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;
}

示例

Main.cpp
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

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 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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。