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

STL算法 | std::max的使用方法总结,std::min 的使用方法与之相同。

(以下以std::max函数为例讲解)
std::max 定义于头文件 <algorithm>,该函数函数原型有主要有以下两个:

  1. 两个数之间比较大小
    1. constexpr const T& max( const T& a, const T& b );
    2. constexpr const T& max( const T& a, const T& b, Compare comp );
      含有谓词的版本
  2. 列表之间比较大小
    1. constexpr T max( std::initializer_list<T> ilist );
    2. constexpr T max( std::initializer_list<T> ilist, Compare comp );
      含有谓词的版本

文章目录

        • 解决 max() 与 std::max() 之间的冲突
      • std::max的使用(std::min使用方法相同)
        • 1. 简单按照四种函数原型使用
        • 2. 两个容器之间的大小比较——集合
        • 3. 两个容器之间的大小比较——集合<自定义的类>
        • 4. 两个容器之间的大小比较——vector

解决 max() 与 std::max() 之间的冲突

在除过<algorithm>头文件以外的其他头文件中,也是有着max()或min()函数的定义的。

比如C的标椎函数库<stdlib.h>、以及在其他一些库中如<minwindef.h>中都有定义,它们一般是通过宏函数的方式定义的,其定义如下:

#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))

而如果我们包含了含有这些 max 或 min 定义的头文件,那么我们在使用<algorithm>库中提供的std::max()时就会冲突,编译器报错: “C2589 “(”:“::”右边的非法标记

而解决的方法也非常简单,我们只需要在 std::max 上加上括号,然后在调用即可。

(std::max)(1, 2);

std::max的使用(std::min使用方法相同)

以下实例代码多参考自 docs.microsoft.com 中的示例 。

1. 简单按照四种函数原型使用
#include <iostream>
#include <algorithm>using namespace std;/*constexpr const T& max( const T& a, const T& b );constexpr const T& max( const T& a, const T& b, Compare comp );两个值与二元谓词constexpr T max( std::initializer_list<T> ilist );constexpr T max( std::initializer_list<T> ilist, Compare comp );列表与二元谓词*/// 返回elem1的绝对值是否大于elem2的绝对值 
bool abs_greater(int elem1, int elem2)
{if (elem1 < 0)elem1 = -elem1;if (elem2 < 0)elem2 = -elem2;return elem1 < elem2;
};int main()
{int a = 6, b = -7;// 返回绝对值较大的整数const int& result1 = max(a, b, abs_greater);// 返回较大的整数const int& result2 = max(a, b);cout << "Using integers 6 and -7..." << endl;cout << "The integer with the greater absolute value is: "<< result1 << "." << endl;cout << "The integer with the greater value is: "<< result2 << "." << endl;cout << endl;int c = -10, d = 8;// 比较列表的成员const int& result3 = max({ a, b, c, d });const int& result4 = max({ a, b, c, d }, abs_greater);cout << "Comparing the members of an initializer_list..." << endl;cout << "The member with the greater value is: " << result3 << endl;cout << "The integer with the greater absolute value is: " << result4 << endl;
}

输出结果:

Using integers 6 and -7...
The integer with the greater absolute value is: -7.
The integer with the greater value is: 6.Comparing the members of an initializer_list...
The member with the greater value is: 8
The integer with the greater absolute value is: -10
2. 两个容器之间的大小比较——集合
// 集合的比较 max({a,b}, {b,c})
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>using namespace std;int main()
{// 使用max算法比较具有int类型元素的set容器int c1 = 1, c2 = 2, c3 = 3;	// 三个int对象set<int> s1, s2, s3;			// 三个set对象// s1中集合中有 {1, 2},s2集合中有{2, 3}s1.insert(1);s1.insert(2);s2.insert(2);s2.insert(3);cout << "s1 = { ";std::copy(s1.begin(), s1.end(), ostream_iterator<int>(std::cout, " "));cout << "}" << endl;cout << "s2 = { ";std::copy(s2.begin(), s2.end(), ostream_iterator<int>(std::cout, " "));cout << "}" << endl;s3 = std::max(s1, s2);cout << "s3 = max(s1, s2) = { ";std::copy(s3.begin(), s3.end(), ostream_iterator<int>(std::cout, " "));cout << "}" << endl;
}

输出结果:

s1 = { 1 2 }
s2 = { 2 3 }
s3 = max(s1, s2) = { 2 3 }
3. 两个容器之间的大小比较——集合<自定义的类>
#include <iterator>
#include <ostream>using namespace std;class CInt
{
public:CInt(int n = 0) : m_nVal(n) {}bool operator<(const CInt& rhs) const{return (m_nVal < rhs.m_nVal);}friend ostream& operator<<(ostream& osIn, const CInt& rhs);
private:int m_nVal;
};// 重载输出,形如 "CInt( 2 )"
inline ostream& operator<<(ostream& osIn, const CInt& rhs)
{osIn << "CInt( " << rhs.m_nVal << " )";return osIn;
}int main()
{// 使用max算法比较具有CInt类型元素的set容器CInt c1 = 1, c2 = 2, c3 = 3;set<CInt> s1, s2, s3;s1.insert(c1);s1.insert(c2);s2.insert(c2);s2.insert(c3);cout << "s1 = { ";std::copy(s1.begin(), s1.end(), ostream_iterator<CInt>(std::cout, " "));cout << "}" << endl;cout << "s2 = { ";std::copy(s2.begin(), s2.end(), ostream_iterator<CInt>(std::cout, " "));cout << "}" << endl;s3 = max(s1, s2);cout << "s2 = max(s1, s2) = { ";std::copy(s3.begin(), s3.end(), ostream_iterator<CInt>(std::cout, " "));cout << "}" << endl;
}

输出结果:

s1 = { CInt( 1 ) CInt( 2 ) }
s2 = { CInt( 2 ) CInt( 3 ) }
s2 = max(s1, s2) = { CInt( 2 ) CInt( 3 ) }
4. 两个容器之间的大小比较——vector
// 两vector之间比较
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>using namespace std;int main()
{// 使用max算法比较向量和整数元素vector <int> v1, v2, v3, v4, v5;/* 覆盖式的分配新值。可以修改向量的大小注意区分assign与resize,后者是插入或擦除 */v1.assign(3, 1);			// {1,1,2}v2 = vector<int>{ 1,2,3 };	// {1,2,3}v3.assign(v2.begin(), prev(v2.end()));// {1,2}cout << "Vector v1 is ( ";for (auto v : v1) cout << v << " ";cout << ")." << endl;cout << "Vector v2 is ( ";for (auto v : v2) cout << v << " ";cout << ")." << endl;cout << "Vector v3 is ( ";for (auto v : v3) cout << v << " ";cout << ")." << endl;v4 = max(v1, v2);v5 = max(v1, v3);cout << "Vector v4 = max (v1,v2) is ( ";for (auto v : v4) cout << v << " ";cout << ")." << endl;cout << "Vector v5 = max (v1,v3) is ( ";for (auto v : v5) cout << v << " ";cout << ")." << endl;
}

输出结果:

Vector v1 is ( 1 1 1 ).
Vector v2 is ( 1 2 3 ).
Vector v3 is ( 1 2 ).
Vector v4 = max (v1,v2) is ( 1 2 3 ).
Vector v5 = max (v1,v3) is ( 1 2 ).
http://www.lryc.cn/news/2414273.html

相关文章:

  • 电脑缺失msvcr120.dll丢失的5种修复,轻松解决msvcr120.dll问题
  • win7怎么看电脑配置和型号参数
  • IIS错误:-2147467259 (0x80004005) 解决方法
  • AttributeSet api分析
  • 各国google网站
  • 【Linux篇】Linux操作系统各部分详解
  • 《炬丰科技-半导体工艺》准原子层蚀刻的硅氮化物
  • css 绝对定位元素居中
  • 麒麟v10 Ubuntu miniconda3 gdal 发布flask web项目
  • CSDN免积分下载
  • CherryUSB 中的 XHCI 驱动分析
  • 继电保护常用元件——电流和电压互感器
  • USBPRINT打印机发指令工具命令行版本 xpt
  • 《通信工程制图与概预算》
  • stagefright 架构分析(一) stagefright 介绍
  • sim3相较于se3的好处
  • 百度分享代码
  • Socket Error # 10054
  • windows update更新返回错误码统计(WUSA.exe)
  • Windows系统的VPS做301重定向新手操作详解
  • pwntools:类型转换
  • mysql distinct和group by以及having用法
  • 信号频谱、幅度、功率和能量
  • 基于深度学习方法的图像分割
  • Windows+C语言 共享内存与互斥量
  • C#之读取数据:DataReader对象
  • 找资源网站
  • [QQAI机器人]-接入腾讯AI接口
  • 转让对战平台~~
  • Windows 11中无法使用小键盘的问题及其解决方法