min-element & max_element
C++ STL之min_element()与max_element()(取容器中的最大最小值)
min_element()和max_element
头文件:#include<algorithm>
作用:返回容器中最小值和最大值。max_element(first,end,cmp);其中cmp为可选择参数!
闲言少叙,上代码,一看就懂:
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int num[]={2,3,1,6,4,5};
cout<<"最小值是 "<<*min_element(num,num+6)<<endl;
cout<<"最大值是 "<<*max_element(num,num+6)<<endl;
cout<<"最小值是 "<<*min_element(num,num+6,cmp)<<endl;
cout<<"最大值是 "<<*max_element(num,num+6,cmp)<<endl;
return 0;
}
转载自:https://www.cnblogs.com/ECJTUACM-873284962/p/6734225.html