很多人知道C++中max(),min(),函数的二数比较,即max(a, b),min(a, b)。但很多人不知道有多个数如何利用该自带的函数比较,这里给出方法:只需要在max(),min()函数内用一个大括号括住自己想要比较的数的集合即可,同理,大括号内是自己提前定义的变量也能进行比较
#include<bits/stdc++.h>
using namespace std;
int main()
{
//取多数的最大值
int maxx = max({1, 2, 3, 4, 5, 6});
//取多数的最小值
int minn = min({1, 2, 3, 4, 5, 6});
cout << "max: " << maxx << endl;
cout << "min: " << minn << endl;
system("pause");
return 0;
}
运行结果: