max_element()与min_element()

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int i,int j)
{
return i<j;
}
struct myclass
{
bool operator()(int i,int j)
{
return i<j;
}
}myobj;
int main()
{
int a[] = {7,1,6,4,9,2};
//不用cmp参数
cout << *min_element(a,a+6)<<endl;//1
cout << *max_element(a,a+6)<<endl;//9
//用方法做cmp参数
cout << *min_element(a,a+6,cmp)<<endl;//1
cout << *max_element(a,a+6,cmp)<<endl;//9
//用结构体做cmp参数
cout << *min_element(a,a+6,myobj)<<endl;//1
cout << *max_element(a,a+6,myobj)<<endl;//9
return 0;
}

猜你喜欢

转载自www.cnblogs.com/knmxx/p/9495036.html