【C++】max_element()以及min_element()

0x00 前言

最后更新日期:2022-09-26

0x01 max_element()以及min_element()

max_element()用于从范围[first, last)中获取最大值的元素。

min_element()用于从范围[first, last)中获取最小值的元素。

1.max_element()

max_element()包含在 algorithm 库中,语法如下(C++17起):

#include<algorithm>
template< class ExecutionPolicy, class ForwardIt, class Compare >
template< class ForwardIt, class Compare >
constexpr ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp );

参数:
first, last:定义要检验范围的向前迭代器 .
comp:比较函数对象

返回值:
指向范围 [first, last) 中最大元素的迭代器。若范围中有多个元素等价于最大元素,则返回指向首个这种元素的迭代器。若范围为空则返回 last

例1:

#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
 
static bool abs_compare(int a, int b)
{
    return (std::abs(a) < std::abs(b)); //绝对值大小比较
}
 
int main()
{
    std::vector<int> v{ 3, 1, -14, 1, 5, 9 }; 
    std::vector<int>::iterator result;
 
    result = std::max_element(v.begin(), v.end());
    std::cout << "max element at: " << std::distance(v.begin(), result) << '\n';
 
    result = std::max_element(v.begin(), v.end(), abs_compare);
    std::cout << "max element (absolute) at: " << std::distance(v.begin(), result) << '\n';
}

输出:

max element at: 5
max element (absolute) at: 2

2. min_element()

min_element()包含在 algorithm 库中,语法如下(C++17起):

#include<algorithm>
template< class ForwardIt, class Compare >
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );

参数:
first, last:定义要检验范围的向前迭代器 .
comp:比较函数对象

返回值:
指向范围 [first, last) 中最小元素的迭代器。若范围中有多个元素等价于最小元素,则返回指向首个这种元素的迭代器。若范围为空则返回 last

例2:

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
 
    std::vector<int>::iterator result = std::min_element(std::begin(v), std::end(v));
    std::cout << "min element at: " << std::distance(std::begin(v), result);
}

输出:

min element at: 1

3.其他示例:

例3:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
		int ans = 0;
		
    vector<int> res{ 1, 2, 3, 4, 2 };
    int arr[] = { 1, 2, 3, 4, 2 };
   
    ans= *max_element(res.begin(), res.end());
    cout << "max element of the vector:" << ans<< endl;
    
    ans= *max_element(arr + 0, arr + 5);
    cout << "max element of the array:" << ans<< endl;
    
    ans= *min_element(res.begin(), res.end());
    cout << "min element of the vector:" << ans<< endl;
    
    ans= *min_element(arr + 0, arr + 5);
    cout << "min element of the array:" << ans<< endl;
    return 0;
}

输出:

max element of the vector:4
max element of the array:4
min element of the vector:1
min element of the array:1

例4:
leetcode有一个可以利用到max_element()函数解题的题目:
https://leetcode.cn/problems/longest-subarray-with-maximum-bitwise-and/

题解:

class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int mx = *max_element(nums.begin(), nums.end());
        int cur = 0, res = 0;
        for (auto &x : nums) {
            if (x == mx) cur++;
            else cur = 0;
            res = max(res, cur);
        }
        return res;
    }
};

以上。

参考文档:
1.https://www.apiref.com/cpp-zh/cpp/algorithm/max_element.html
2.https://www.apiref.com/cpp-zh/cpp/algorithm/min_element.html
3.https://vimsky.com/examples/usage/stl-std-max_element-function-with-example-02.html

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值