首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::max_element

Defined in header <algorithm>

?

?

?

(1)

?

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

(until C++17)

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

(since C++17)

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

(2)

(since C++17)

?

(3)

?

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

(until C++17)

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

(since C++17)

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

(4)

(since C++17)

找到范围内最大的元素[first, last)...

1%29个元素的比较operator<...

使用给定的二进制比较函数对3%29个元素进行比较。comp...

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

forward iterators defining the range to examine

policy

-

the execution policy to use. See execution policy for details.

cmp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than the second. 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 object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

---。

返回值

最伟大的元素在范围内的迭代器[first, last)如果范围中的几个元素等效于最大的元素,则将迭代器返回到第一个这样的元素。回报last如果范围是空的。

复杂性

精确的最大百分比28N-1,0%29比较,其中N =std::distance(first, last)...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<class ForwardIt>前进最大[医]元素%28 Forwardit First,Forwardit持久%29{if%281==最后%29{返回最后;}Forwardit最大值=First;++First;for%28;First%21=Lest;++First%29{if%28%2A最大<%2A第一%29{最大=第一;}返回最大;}

第二版

模板<类向前,类比较>向前最大[医]元素%28 Forward First,Forwardit Lest,比较CMP%29{if%28first==最后%29{返回最后;}Forwardit最大=第一;++优先;对于%28;第21%=最后;++第一%29{if%28 cmp%28%2A最大的,%2A第一%29%29{最大=第一;}返回最大;}

二次

代码语言:javascript
复制
#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);
}

二次

产出:

二次

代码语言:javascript
复制
max element at: 5
max element (absolute) at: 2

二次

另见

min_element

returns the smallest element in a range (function template)

minmax_element (C++11)

returns the smallest and the largest elements in a range (function template)

max

returns the greater of the given values (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com