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

std::minmax_element

Defined in header <algorithm>

(1)

template< class ForwardIt > std::pair<ForwardIt,ForwardIt> minmax_element( ForwardIt first, ForwardIt last );

(since C++11) (until C++17)

template< class ForwardIt > constexpr std::pair<ForwardIt,ForwardIt> minmax_element( ForwardIt first, ForwardIt last );

(since C++17)

template< class ExecutionPolicy, class ForwardIt > std::pair<ForwardIt,ForwardIt> minmax_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );

(2)

(since C++17)

(3)

template< class ForwardIt, class Compare > std::pair<ForwardIt,ForwardIt> minmax_element( ForwardIt first, ForwardIt last, Compare comp );

(since C++11) (until C++17)

template< class ForwardIt, class Compare > constexpr std::pair<ForwardIt,ForwardIt> minmax_element( ForwardIt first, ForwardIt last, Compare comp );

(since C++17)

template< class ExecutionPolicy, class ForwardIt, class Compare > std::pair<ForwardIt,ForwardIt> minmax_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Compare comp );

(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 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 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. ​

类型要求

---。

返回值

由一个迭代器到最小元素作为第一个元素和一个迭代器到最大元素作为第二个元素组成的一对。回报std::make_pair(first, first)如果范围是空的。如果多个元素等效于最小的元素,则返回第一个这样的元素的迭代器。如果多个元素等效于最大的元素,则返回到最后一个这样的元素的迭代器。

复杂性

最多最大为28层%283/2%28N-1%29%29,0%29个谓词应用程序,其中N =std::distance(first, last)...

例外

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

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

注记

这个算法不同于std::make_pair(std::min_element(),std::max_element()),不仅在效率上,而且在此算法中发现最后最大要素std::max_element发现第一最大的元素。

可能的实施

第一版

*。

模板<class ForwardIt>STD:配对<向前,向前>最小[医]元素%28 ForwardIt First,Forwardit Lest%29{返回std::minmax[医]元素%281,最后,std::减<>%28%29%29;}

第二版

模板<类向前,类比较>std::对<Forwardit,Forwardit>minmax[医]元素%28 Forwardit First,Forwardit Lest,比较comp%29{std:对<Forwardit,Forwardit>结果%281,首先%29;if%28first==最后%29返回结果;if%28++first==最后%29返回结果;if%28comp%28%2A首先,%2A结果:第一%29%29{结果第一;}其他{结果第二;}而%28++第21%=最后%29{Forwardit i=第一;if%28++First=最后%29{if%28 comp%28%2A我,%2A结果:第一%29%29结果.First=i;否则,如果%28%21%28 comp%28%2A我,%2A结果第二,%29%29%29结果.第二=i;断开;}{if%28 comp%28%2A首先,%2AI%29%29(如果%28)%2A首先,%2A结果:第一%29%29结果第一=第一;如果%28%21%28 Comp%28%2A我,%2A结果第二%29%29%29结果.第二=i;}{if%28 comp%28%2A我,%2A结果:第一%29%29结果.第一=i;如果%28%21%28 Comp%28%2A首先,%2A结果:第二%29%29%29结果.第二=第一;}}返回结果;}

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v = { 3, 9, 1, 4, 2, 5, 9 };
 
    auto result = std::minmax_element(v.begin(), v.end());
    std::cout << "min element at: " << (result.first - v.begin()) << '\n';
    std::cout << "max element at: " << (result.second - v.begin()) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
min element at: 2
max element at: 6

二次

另见

min_element

returns the smallest element in a range (function template)

max_element

returns the largest element in a range (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券