Namespaces
Variants

std::min_element

来自 zh.cppreference.net
 
 
算法库
受约束算法与范围上的算法 (C++20)
受约束算法,例如 ranges::copy, ranges::sort, ...
排序及相关操作
划分操作
(C++11)    

排序操作
二分查找操作
(在划分后范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <algorithm>
template< class ForwardIt > 
ForwardIt min_element( ForwardIt first, ForwardIt last );
(1) (constexpr 起 C++17)
template< class ExecutionPolicy, class ForwardIt > 
ForwardIt min_element( ExecutionPolicy&& policy, 
                       ForwardIt first, ForwardIt last );
(2) (自 C++17)
template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last,
                       Compare comp );
(3) (constexpr 起 C++17)
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt min_element( ExecutionPolicy&& policy, 
                       ForwardIt first, ForwardIt last,
                       Compare comp );
(4) (自 C++17)

在范围 [first, last) 中查找最小元素。

1) 使用 operator<(直至 C++20)std::less{}(自 C++20)。
3) 使用比较函数 comp。
2,4) 同 (1,3),但按照 policy。
这些重载仅当以下表达式的值为 true:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(直至 C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(自 C++20)

参数

first, last - 定义要检查的元素 范围 的迭代器对
policy - 要使用的 执行策略
comp - 比较函数对象(即满足 Compare 要求的对象),返回 true 如果第一个参数 小于 第二个。

比较函数的签名应等价于以下内容:

bool cmp(const Type1& a, const Type2& b);

虽然签名不需要有 const&,但函数不能修改传递给它的对象,并且必须能够接受类型(可能为 const) Type1 和 Type2 的所有值,无论 值类别(因此,Type1& 是不允许的,Type1 也不允许,除非对于 Type1 移动等同于复制(自 C++11))。
类型 Type1 和 Type2 必须满足:类型 ForwardIt 的对象可以解引用并隐式转换为两者。

类型要求
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。

返回值

指向范围 [first, last) 中最小的元素的迭代器。如果范围内有多个元素等价于最小元素,则返回指向第一个这样的元素的迭代器。如果范围为空,则返回 last。

复杂度

给定 N 为 std::distance(first, last):

1,2) 恰好 max(N-1,0) 次比较使用 operator<(直至 C++20)std::less{}(自 C++20)。
3,4) 恰好 max(N-1,0) 次比较函数的应用 comp。

异常

带有模板参数名 ExecutionPolicy 的重载报告错误如下:

  • 如果作为算法一部分调用的函数执行时抛出异常,且 ExecutionPolicy 是 标准策略 之一,则 std::terminate 被调用。对于任何其他 ExecutionPolicy,行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc。

可能的实现

min_element (1)
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
    
    ForwardIt smallest = first;
    
    while (++first != last)
        if (*first < *smallest)
            smallest = first;
    
    return smallest;
}
min_element (3)
template<class ForwardIt, class Compare>
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp)
{
    if (first == last)
        return last;
    
    ForwardIt smallest = first;
    
    while (++first != last)
        if (comp(*first, *smallest))
            smallest = first;
    
    return smallest;
}

示例

#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(v.begin(), v.end());
    std::cout << "min element has value " << *result << " and index ["
              << std::distance(v.begin(), result) << "]\n";
}

输出:

min element has value -4 and index [2]

缺陷报告

以下行为变更缺陷报告已回溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 212 C++98 如果 [first, last) 为空,则未指定返回值 返回 last 在此情况下
LWG 2150 C++98 返回了指向第一个非最大元素的迭代器 更正了返回值

参阅

返回范围中的最大元素
(函数模板 & 算法函数对象)
返回范围中的最小和最大元素
(函数模板 & 算法函数对象)
返回给定值中的较小值
(函数模板 & 算法函数对象)
返回范围中的最小元素
(算法函数对象)