C++ STL之min_element()與max_element()(取容器中的最大最小值)能夠用來找一個最大值和最小值;

 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 }
相關文章
相關標籤/搜索