std::ranges:: min_element
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
|
template
<
std::
forward_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
std
::
projected
<
I, Proj
>>
Comp
=
ranges::
less
>
|
(1) | (C++20 起) |
|
template
<
ranges::
forward_range
R,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(2) | (C++20 起) |
[
first
,
last
)
中查找最小元素。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| first, last | - | 定义待检查元素范围的 迭代器-哨位 对 |
| r | - |
待检查的
range
|
| comp | - | 应用于投影元素的比较器 |
| proj | - | 应用于元素的投影器 |
返回值
指向范围
[
first
,
last
)
中最小元素的迭代器。若范围内存在多个等价的最小元素,则返回首个此类元素的迭代器。若范围为空(即
first
==
last
),则返回与
last
相等的迭代器。
复杂度
恰好需要 max(N - 1, 0) 次比较,其中 N = ranges:: distance ( first, last ) 。
可能的实现
struct min_element_fn { template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_strict_weak_order<std::projected<I, Proj>> Comp = ranges::less> constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { if (first == last) return last; auto smallest = first; while (++first != last) if (std::invoke(comp, std::invoke(proj, *first), std::invoke(proj, *smallest))) smallest = first; return smallest; } template<ranges::forward_range R, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less> constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj)); } }; inline constexpr min_element_fn min_element; |
示例
#include <algorithm> #include <array> #include <cmath> #include <iostream> int main() { namespace ranges = std::ranges; std::array v{3, 1, -13, 1, 3, 7, -13}; auto iterator = ranges::min_element(v.begin(), v.end()); auto position = ranges::distance(v.begin(), iterator); std::cout << "min element is v[" << position << "] == " << *iterator << '\n'; auto abs_compare = [](int a, int b) { return (std::abs(a) < std::abs(b)); }; iterator = ranges::min_element(v, abs_compare); position = ranges::distance(v.begin(), iterator); std::cout << "|min| element is v[" << position << "] == " << *iterator << '\n'; }
输出:
min element is v[2] == -13 |min| element is v[1] == 1
参见
|
(C++20)
|
返回范围中的最大元素
(算法函数对象) |
|
(C++20)
|
返回范围中的最小和最大元素
(算法函数对象) |
|
(C++20)
|
返回给定值中的较大者
(算法函数对象) |
|
返回范围中的最小元素
(函数模板) |