Namespaces
Variants

std:: min

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
min
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class T >
const T & min ( const T & a, const T & b ) ;
(1) (C++14 起为 constexpr)
template < class T, class Compare >
const T & min ( const T & a, const T & b, Compare comp ) ;
(2) (C++14 起为 constexpr)
template < class T >
T min ( std:: initializer_list < T > ilist ) ;
(3) (C++11 起)
(C++14 起为 constexpr)
template < class T, class Compare >
T min ( std:: initializer_list < T > ilist, Compare comp ) ;
(4) (C++11 起)
(C++14 起为 constexpr)

返回给定值中的较小值。

1,2) 返回 a b 中的较小值。
1) 使用 operator < 来比较数值。
如果 T 不满足 LessThanComparable 要求,则行为未定义。
2) 使用比较函数 comp 来比较数值。
3,4) 返回初始化列表 ilist 中的最小值。
如果 ilist. size ( ) 为零,或 T 不满足 CopyConstructible 要求,则行为未定义。
3) 使用 operator < 来比较数值。
如果 T 不满足 LessThanComparable 要求,则行为未定义。
4) 使用比较函数 comp 来比较数值。

目录

参数

a, b - 要比较的值
ilist - 包含待比较值的初始化列表
cmp - 比较函数对象(即满足 Compare 要求的对象),当 a 小于 b 时返回 true

比较函数的签名应等价于如下形式:

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

虽然签名不需要包含 const & ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许使用 Type1 & ,也不允许使用 Type1 ,除非对于 Type1 类型移动操作等同于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 T 类型的对象能隐式转换到这两个类型。

返回值

1,2) 返回 a b 中的较小值。若两值相等,则返回 a
3,4) ilist 中的最小值。若有多个值等于最小值,则返回最左侧的值。

复杂度

1) 仅使用一次 operator < 进行比较。
2) 仅进行一次比较函数 comp 的应用。
3,4) 给定 N ilist. size ( )
3) 恰好进行 N-1 次使用 operator < 的比较。
4) 恰好需要 N-1 次比较函数 comp 的应用。

可能的实现

min (1)
template<class T>
const T& min(const T& a, const T& b)
{
    return (b < a) ? b : a;
}
min (2)
template<class T, class Compare>
const T& min(const T& a, const T& b, Compare comp)
{
    return (comp(b, a)) ? b : a;
}
min (3)
template<class T>
T min(std::initializer_list<T> ilist)
{
    return *std::min_element(ilist.begin(), ilist.end());
}
min (4)
template<class T, class Compare>
T min(std::initializer_list<T> ilist, Compare comp)
{
    return *std::min_element(ilist.begin(), ilist.end(), comp);
}

注释

当某个参数是临时对象且该参数被返回时,通过引用捕获 std::min 的结果会产生悬垂引用:

int n = -1;
const int& r = std::min(n + 2, n * 2); // r 成为悬垂引用

示例

#include <algorithm>
#include <iostream>
#include <string_view>
int main()
{
    std::cout << "10 与 010 中较小的是 " << std::min(10, 010) << '\n'
              << "'d' 与 'b' 中较小的是 '" << std::min('d', 'b') << "'\n"
              << "\"foo\"\"bar\"\"hello\" 中最短的是 \""
              << std::min({"foo", "bar", "hello"},
                          [](const std::string_view s1, const std::string_view s2)
                          {
                              return s1.size() < s2.size();
                          }) << "\"\n";
}

输出:

10 与 010 中较小的是 8
'd' 与 'b' 中较小的是 'b'
"foo"、"bar" 和 "hello" 中最短的是 "foo"

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 281 C++98 要求 T 满足 CopyConstructible 对于重载 ( 1,2 ) 无需此要求
LWG 2239 C++98
C++11
1. 要求 T 满足 LessThanComparable 对于
重载 ( 2 ) (C++98) 和 ( 4 ) (C++11)
2. 缺失复杂度要求
1. 无需此要求
2. 补充了要求

参见

返回给定值中的较大者
(函数模板)
(C++11)
返回两个元素中的较小者和较大者
(函数模板)
返回范围内的最小元素
(函数模板)
(C++17)
将值限制在一对边界值之间
(函数模板)
返回给定值中的较小者
(算法函数对象)