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

std::max

STD:最多

Defined in header
(1)
template< class T > const T& max( const T& a, const T& b (until C++14)
template< class T > constexpr const T& max( const T& a, const T& b (since C++14)
(2)
template< class T, class Compare > const T& max( const T& a, const T& b, Compare comp (until C++14)
template< class T, class Compare > constexpr const T& max( const T& a, const T& b, Compare comp (since C++14)
(3)
template< class T > T max( std::initializer_list<T> ilist (since C++11) (until C++14)
template< class T > constexpr T max( std::initializer_list<T> ilist (since C++14)
(4)
template< class T, class Compare > T max( std::initializer_list<T> ilist, Compare comp (since C++11) (until C++14)
template< class T, class Compare > constexpr T max( std::initializer_list<T> ilist, Compare comp (since C++14)

返回给定值中较大的值。

1-2) 返回ab中较大的值

3-4) 返回初始化程序列表ilist中最大的值

(1,3)版本使用operator<比较值,(2,4)版本使用给定的比较函数comp

参数

a, b-the values to compare
ilist-initializer list with the values to compare
comp-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 T can be implicitly converted to both of them. ​

类型要求

-T必须符合LessThanable的要求,才能使用过载%281,3%29。

-T必须符合CopyConstrucable的要求,才能使用重载%283,4%29。

返回值

1-2) 如果ab相等,则返回a

3-4%29ilist如果多个值等效于最大值,则返回最左边的值。

复杂性

1-2%29正是一个比较

3-4%29ilist.size() - 1比较

可能的实施

第一版

*。

模板<class T>T&max%28 const T&a,Const T&b%29{返回%28a<b%29?B:A;}

第二版

模板<T类,类比较>Const T&max%28 const T&a,Const T&b,比较comp%29{返回%28 comp%28a,b%29%29?B:A;}

第三版

模板<class T>Tmax%28 std::初始值[医]列单<T>反式%29{返回%2ASTD:最多[医]元素%28 ilist.start%28%29,ilist.end%28%29%29;}

第四版

模板<类T,类比较>Tmax%28 std::初始值[医]列单<T>反列表,比较Comp%29{返回%2ASTD:最多[医]元素%28 ilist.start%28%29,ilist.end%28%29,comp%29;}

注记

捕获结果std::max通过引用,如果其中一个参数是rvalue,则如果返回该参数,则生成一个悬空引用:

二次

int n = 1; const int& r = std::max(n-1, n+1 // r is dangling

二次

二次

#include <algorithm> #include <iostream> #include <string> int main() { std::cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n' << "larger of 'a', and 'b': " << std::max('a', 'b') << '\n' << "longest of \"foo\", \"bar\", and \"hello\": " << std::max( { "foo", "bar", "hello" }, [](const std::string& s1, const std::string& s2) { return s1.size() < s2.size( }) << '\n'; }

二次

产出:

二次

larger of 1 and 9999: 9999 larger of 'a', and 'b': b longest of "foo", "bar", and "hello": hello

二次

另见

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

© cppreference.com

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

http://en.cppreference.com/w/cpp/Algorithm/max