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; 
}


 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页