|
max_element寻找一个容器里元素值最大的元素的位置,min_element则是寻找最小的,格式
min_element(x,y,cmp) 返回x到y-1之间按照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+3)<<endl;
- cout<<"最大值是 "<<*max_element(num,num+3)<<endl;
- cout<<"最小值是 "<<*min_element(num,num+6,cmp)<<endl;
- cout<<"最大值是 "<<*max_element(num,num+6,cmp)<<endl;
- return 0;
- }
复制代码
|
|