C++ STL之min_element()与max_element()(取容器中的最大最小值)

min_element()和max_element

头文件:#include<algorithm>

作用:返回容器中最小值和最大值。max_element(first,end,cmp);其中cmp为可选择参数!

 

闲言少叙,上代码,一看就懂:

 1 #include<iostream>  
 2 #include<algorithm>  
 3 using namespace std;  
 4 bool cmp(int a,int b)  
 5 {  
 6       return a<b;  
 7 }  
 8 int main()  
 9 {  
10       int num[]={2,3,1,6,4,5};  
11       cout<<"最小值是 "<<*min_element(num,num+6)<<endl;  
12       cout<<"最大值是 "<<*max_element(num,num+6)<<endl;  
13       cout<<"最小值是 "<<*min_element(num,num+6,cmp)<<endl;  
14       cout<<"最大值是 "<<*max_element(num,num+6,cmp)<<endl;  
15       return 0;   
16 }  

 

posted @ 2017-04-19 16:43  Angel_Kitty  阅读(19751)  评论(2编辑  收藏  举报