头文件:#include
作用:返回容器中最小值和最大值。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+3,num+6,cmp)<<endl;
14 cout<<"最大值是 "<<*max_element(num+3,num+6,cmp)<<endl;
15 return 0;
16 }
结果为
最小值是 1
最大值是 6
最小值是 4
最大值是 6
slam14讲中的例子:
1 float min_dis = std::min_element (
2 matches.begin(), matches.end(),
3 [] ( const cv::DMatch& m1, const cv::DMatch& m2 )
4 {
5 return m1.distance < m2.distance;
6 } )->distance;