#include <iostream>
using
namespace
std;
int
main()
{
cout<<max(12,13)<<endl;
return
0;
}
|
可以直接调用,这里输出13
追问:
1.如果两个相等呢?2.如果里面的参数个数不确定呢?这时还可以么?Thx
追答:
这个函数的实现是这样的:
template
<
class
_Ty>
inline
const
_Ty& (max)(
const
_Ty& _Left,
const
_Ty& _Right)
{
// return larger of _Left and _Right
return
(_DEBUG_LT(_Left, _Right) ? _Right : _Left);
}
|
相等的话就等于这个值。max(12,12)=12 只能有两个参数
参数个数不确定就得用数组了,比如int a[]={12,13,14,15};
#include<algorithm>//这里包含数组选取最大值和最小值的函数
cout<<*max_element(a,a+4)<<endl; //使用这个函数求出数组里的最大值