std::min
STD::MIN
Defined in header | | |
---|---|---|
| (1) | |
template< class T > const T& min( const T& a, const T& b | (until C++14) | |
template< class T > constexpr const T& min( const T& a, const T& b | (since C++14) | |
| (2) | |
template< class T, class Compare > const T& min( const T& a, const T& b, Compare comp | (until C++14) | |
template< class T, class Compare > constexpr const T& min( const T& a, const T& b, Compare comp | (since C++14) | |
| (3) | |
template< class T > T min( std::initializer_list<T> ilist | (since C++11) (until C++14) | |
template< class T > constexpr T min( std::initializer_list<T> ilist | (since C++14) | |
| (4) | |
template< class T, class Compare > T min( std::initializer_list<T> ilist, Compare comp | (since C++11) (until C++14) | |
template< class T, class Compare > constexpr T min( std::initializer_list<T> ilist, Compare comp | (since C++14) |
返回给定值中较小的值。
1-2%29返回较小的a
和b
...
3-4%29返回初始化程序列表中最小的值。ilist
...
%281,3%29版本使用operator<为了比较这些值,%282,4%29版本使用给定的比较函数comp...
参数
a, b | - | the values to compare |
---|---|---|
ilist | - | initializer list with the values to compare |
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 T can be implicitly converted to both of them. |
类型要求
-T必须符合LessThanable的要求,才能使用过载%281,3%29。
-T必须符合CopyConstrucable的要求,才能使用重载%283,4%29。
返回值
1-2%29a
和b
如果值相等,则返回a
...
3-4%29最小值ilist
如果多个值等效于最小值,则返回最左边的此类值。
复杂性
1-2%29正是一个比较
3-4%29ilist.size() - 1
比较
可能的实施
第一版
*。
模板<class T>T&min%28 const T&a,Const T&b%29{返回%28b<a%29?B:A;}
第二版
模板<T类,类比较>Const T&min%28 const T&a,Const T&b,比较comp%29{返回%28 comp%28b,a%29%29?B:A;}
第三版
模板<class T>T min%28 STD::初始化器[医]列单<T>反式%29{返回%2ASTD::MIN[医]元素%28 ilist.start%28%29,ilist.end%28%29%29;}
第四版
模板<类T,类比较>T min%28 std::初始值[医]列单<T>反列表,比较Comp%29{返回%2ASTD::MIN[医]元素%28 ilist.start%28%29,ilist.end%28%29,comp%29;}
警告
捕获结果std::min
通过引用,如果其中一个参数是rvalue,则如果返回该参数,则生成一个悬空引用:
二次
int n = 1;
const int& r = std::min(n-1, n+1
// r is dangling
二次
例
二次
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::cout << "smaller of 1 and 9999: " << std::min(1, 9999) << '\n'
<< "smaller of 'a', and 'b': " << std::min('a', 'b') << '\n'
<< "shortest of \"foo\", \"bar\", and \"hello\": " <<
std::min( { "foo", "bar", "hello" },
[](const std::string& s1, const std::string& s2) {
return s1.size() < s2.size(
}) << '\n';
}
二次
产出:
二次
smaller of 1 and 9999: 1
smaller of 'a', and 'b': a
shortest of "foo", "bar", and "hello": foo
二次
另见
max | returns the greater of the given values (function template) |
---|---|
minmax (C++11) | returns the smaller and larger of two elements (function template) |
min_element | returns the smallest element in a range (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。