在线文档教程
C++
算法 | Algorithm

std::min_element

STD::MIN[医]元素

Defined in header
(1)
template< class ForwardIt > ForwardIt min_element( ForwardIt first, ForwardIt last (until C++17)
template< class ForwardIt > constexpr ForwardIt min_element( ForwardIt first, ForwardIt last (since C++17)
template< class ExecutionPolicy, class ForwardIt > ForwardIt min_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last (2)(since C++17)
(3)
template< class ForwardIt, class Compare > ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp (until C++17)
template< class ForwardIt, class Compare > constexpr ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp (since C++17)
template< class ExecutionPolicy, class ForwardIt, class Compare > ForwardIt min_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 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. ​

类型要求

---。

返回值

范围内最小元素的迭代器[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返回最后;向前最小=优先;++第一;对于%28;第一%21=最后;++第一%29{如果%28%2A第一<%2A最小%29{最小=先;}返回最小;}

第二版

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

二次

#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{3, 1, 4, 1, 5, 9}; std::vector<int>::iterator result = std::min_element(std::begin(v), std::end(v) std::cout << "min element at: " << std::distance(std::begin(v), result }

二次

产出:

二次

min element at: 1

二次

另见

max_elementreturns the largest element in a range (function template)
minmax_element (C++11)returns the smallest and the largest elements in a range (function template)
minreturns the smaller of the given values (function template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/Algorithm/min[医]元素