当前位置: 首页 > news >正文

STL 算法库中的 min_element 和 max_element

在 C++ STL中,min_elementmax_element 是两个非常实用的算法函数,用于快速找到容器或范围内的最小值和最大值,这里以min为例。

头文件<algorithm>
语法

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

template <class ForwardIterator, class Compare>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last, Compare comp);

参数

  • firstlast:定义要搜索的范围 [first, last)
  • comp(可选):自定义比较函数,用于指定排序规则。

时间复杂度为 O ( n ) O(n) O(n),其中 n n n 是范围 [first, last) 中的元素数量。空间复杂度为 O ( 1 ) O(1) O(1),因为只使用常量级别的额外空间。


基本用法

如何使用 min_elementmax_element 找到数组中的最小值和最大值。

#include <iostream>
#include <vector>
#include <algorithm> 

using namespace std;

int main() {
    vector<int> nums = {4, 2, 9, 1, 7, 3};
    auto min_it = min_element(nums.begin(), nums.end());
    auto max_it = max_element(nums.begin(), nums.end());

    if (min_it != nums.end()) {
        cout << "Min: " << *min_it << endl;
    }
    if (max_it != nums.end()) {
        cout << "Max: " << *max_it << endl;
    }

    return 0;
}
Min: 1
Max: 9

自定义比较函数

按绝对值大小来寻找最小值。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath> 

using namespace std;

int main() {
    vector<int> nums = {-4, 2, -9, 1, 7, -3};
    auto min_it = min_element(nums.begin(), nums.end(), [](int a, int b) {
        return abs(a) < abs(b);
    });

    if (min_it != nums.end()) {
        cout << "Min value by absolute value: " << *min_it << endl;
    }

    return 0;
}
Min value by absolute value: 1

同时查找最小值和最大值

如果需要同时查找最小值和最大值,可以结合 minmax_element 使用(这是 min_elementmax_element 的扩展版本)。

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    vector<int> nums = {4, 2, 9, 1, 7, 3};

    auto [min_it, max_it] = minmax_element(nums.begin(), nums.end());

    if (min_it != nums.end() && max_it != nums.end()) {
        cout << "Minimum value: " << *min_it << endl;
        cout << "Maximum value: " << *max_it << endl;
    }

    return 0;
}

输出

Minimum value: 1
Maximum value: 9

容器为空的情况

如果范围 [first, last) 为空,则 min_elementmax_element 返回 last。因此,在使用返回值之前,应检查容器是否为空。

if (!nums.empty()) {
    auto min_it = min_element(nums.begin(), nums.end());
    cout << "Min: " << *min_it << endl;
}

自定义cmp

自定义比较函数必须满足严格弱序(strict weak ordering)。简单来说,如果 cmp(a, b) 为真,则 a 应小于 b。如果 cmp(a, b)cmp(b, a) 都为假,则 ab 相等。

相关文章:

  • 个人网站可以做论坛万能搜索
  • 湖北网站推广宣传影响seo排名的因素
  • 纯静态做企业网站成品ppt网站国外
  • 宝盈集团直营网站怎么做张文宏说上海可能是疫情爆发
  • 管委会网站建设要点百度推广免费
  • 被他人备案后做违法网站北京百度seo
  • 什么是HTTP协议
  • multer 依赖详解
  • USBCANFD接口卡介绍
  • MATLAB应用介绍
  • 前后端分离项目部署到云服务器、宝塔(前端vue、后端springboot)详细教程
  • 深入理解分布式系统中的关键概念:三阶段提交、补偿事务、消息队列与Saga事务模型及分布式ID生成方案
  • C#通过接口 继承接口的类 实现约束 对List内数据类型的值进行排序,可直接复制使用
  • 我通过AI编程完成了第一个实用程序
  • 如何选择消息队列
  • 【开源-线程池(Thread Pool)项目对比】
  • vue cli 与 vite的区别
  • 《当齐天大圣踏入3A游戏世界:黑神话·悟空的破壁传奇》:此文为AI自动生成
  • 【UCB CS 61B SP24】Lecture 16 - Data Structures 2: ADTs, BSTs学习笔记
  • MySQL 8.0 Enterprise Backup (MEB) 备份与恢复实践指南
  • 【Maui】自定义统一色彩样式
  • 【软考-架构】1.2、指令系统-存储系统-cache
  • SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
  • RabbitMQ 高级配置与优化:从入门到精通
  • Linux:同步
  • 说说JVM的底层原理(JAVA是如何运行的)?