STL算法 | std::max的使用方法总结,std::min 的使用方法与之相同。
(以下以std::max函数为例讲解)
std::max 定义于头文件 <algorithm>,该函数函数原型有主要有以下两个:
- 两个数之间比较大小
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 );
含有谓词的版本
文章目录
- 解决 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 ).