当前位置: 首页 > 工具软件 > Element > 使用案例 >

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

马琛
2023-12-01

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

 类似资料: