美文网首页
minmax_element、min_element、max_e

minmax_element、min_element、max_e

作者: 顽强的猫尾草 | 来源:发表于2018-10-11 08:24 被阅读21次

std::minmax_element(C++11)

函数原型:

template< class ForwardIt > 
std::pair<ForwardIt, ForwardIt> minmax_element( ForwardIt first, ForwardIt last );

返回的是两个迭代器组成的 pair。

使用示例:

void findMinMax(vector<int>& nums) {
    auto res = minmax_element(nums.begin(), nums.end());
    cout << "最小值:" << *(res.first);
    cout << "最大值:" << *(res.second);
}

std::min_element(C++17)

函数原型:

template< class ForwardIt > 
ForwardIt min_element( ForwardIt first, ForwardIt last );

使用示例:

void findMin(vector<int>& nums) {
    vector<int>::iterator res = min_element(begin(nums), end(nums));
    cout << "min element at: " << distance(begin(v), res);
}

std::max_element(C++17)

函数原型:

template< class ForwardIt > 
ForwardIt max_element( ForwardIt first, ForwardIt last );

使用示例:

void findMax(vector<int>& nums) {
    vector<int>::iterator res = max_element(begin(nums), end(nums));
    cout << "max element at: " << distance(begin(v), res);
}



以上只列出了最基础的使用方法,更多请参考:
[1] std::minmax_element
[2] std::min_element
[3] std::max_element

相关文章

网友评论

      本文标题:minmax_element、min_element、max_e

      本文链接:https://www.haomeiwen.com/subject/mmuvaftx.html