笔记 C++11 std::minmax_element() 的使用(寻找最小值和最大值)

本文详细介绍了C++标准库中的minmax_element函数,该函数用于寻找容器中最小和最大元素的位置。通过示例展示了如何使用此函数,并对比了min_element和max_element的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 函数的声明

  • 可以看到:返回一个 std::pair<ForwardIt,ForwardIt>pair 对)
template< class ForwardIt >
std::pair<ForwardIt,ForwardIt>
    minmax_element( ForwardIt first, ForwardIt last );

1. 寻找最小值和最大值

  • 使用
  • 顾名思义,从函数名看,minmax_element,先是 min,再 max,所以,返回的类中的数据成员 first 对应着最小值second 对应着最大值
  • 源码
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt> 
    minmax_element(ForwardIt first, ForwardIt last, Compare comp)
{
    auto min = first, max = first;
 
    if (first == last || ++first == last)
        return {min, max};
 
    if (comp(*first, *min)) {
        min = first;
    } else {
        max = first;
    }
 
    while (++first != last) {
        auto i = first;
        if (++first == last) {
            if (comp(*i, *min)) min = i;
            else if (!(comp(*i, *max))) max = i;
            break;
        } else {
            if (comp(*first, *i)) {
                if (comp(*first, *min)) min = first;
                if (!(comp(*i, *max))) max = i;
            } else {
                if (comp(*i, *min)) min = i;
                if (!(comp(*first, *max))) max = first;
            }
        }
    }
    return {min, max};
}
  • 例子:
#include <iostream>

int main() {
    
    std::vector<int> v = { 1, 2, 5, 4, 100, 0, -199, 33 };

    auto result = std::minmax_element(v.begin(), v.end());
    // 输出首次出现的最小元素
    std::cout << "min element is: " << *result.first << '\n';

    // 输出首次出现的最大元素
    std::cout << "max element is: " << *result.second << '\n';

    return 0;
}


/*
// 官网例子
#include <algorithm>
#include <iostream>

int main() {
    
    const auto v = { 3, 9, 1, 4, 2, 5, 9 };
    auto pStart = begin(v);
    auto pEnd = end(v);
    const auto result = std::minmax_element(begin(v), end(v));

    std::cout << "min = " << *result.first << ", max = " << *result.second << '\n';

    return 0;
}
*/

  • 结果:
    在这里插入图片描述

2. 其它类似的函数

1. C++17 中的 min_element, 返回迭代器位置, 复杂度为 O(n)

  • 源码
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
    if (first == last) return last;
 
    ForwardIt smallest = first;
    ++first;
    for (; first != last; ++first) {
        if (*first < *smallest) {
            smallest = first;
        }
    }
    return smallest;
}
  • 例子
#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(v.begin(), v.end());
    std::cout << "min element at: " << std::distance(v.begin(), result) << std::endl;
    std::cout << "min value is: " << *result << std::endl;				// 1

	return 0;
}

2. C++17 中的 max_element, 返回迭代器位置, 复杂度为 O(n)

  • 源码
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last)
{
    if (first == last) return last;
 
    ForwardIt largest = first;
    ++first;
    for (; first != last; ++first) {
        if (*largest < *first) {
            largest = first;
        }
    }
    return largest;
}
  • 例子
#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
 
    std::vector<int>::iterator result = std::max_element(v.begin(), v.end());
    std::cout << "max element at: " << std::distance(v.begin(), result) << std::endl;
    std::cout << "max value is: " << *result << std::endl;					// 9

	return 0;
}

3. 参考

https://en.cppreference.com/w/cpp/algorithm

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值