转接自STL算法
1. min_element() 返回最小值元素
2. max_element() 返回最大值元素
3. minmax_element() 返回最小值和最大值元素 start form c++11
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//1. min_element() 返回最小值元素
//2. max_element() 返回最大值元素
//3. minmax_element() 返回最小值和最大值元素 start form c++11
void test(const vector<int> &vec)
{
auto f = [](const int& a, const int& b)
{
return a < b;
};
//默认Pred使用less<>()
auto min_pos = min_element(vec.begin(), vec.end(),f);
cout <<"the min is " << *min_pos << endl;
//默认Pred使用less<>()
auto max_pos = max_element(vec.begin(), vec.end());
cout << "the max is " << *max_pos << endl;
//默认Pred使用less<>()
auto pair = minmax_element(vec.begin(), vec.end());
cout << "the min is " << *pair.first << endl;
cout << "the max is " << *pair.second << endl;
}
int main()
{
vector<int> vec{ 1,2,3,3,4,5,6,3,7,8,3 };
test(vec);
system("pause");
return 0;
}