STL算法之返回最大值最小值

来源:互联网 发布:mac如何免费翻墙 编辑:程序博客网 时间:2024/06/05 14:14

转接自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++11void 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;}