std::min and std::max
来源:互联网 发布:极客头条php 编辑:程序博客网 时间:2024/04/29 10:52
int t = (std::max)(timeout, lagtime);
Why did I put parentheses around std::max? Because windows.h defines (among other things) a max and a min macro. If you include windows.h the above code will not compile. For example the following:
#include "windows.h"#include <algorithm>void foo() { int i = 5; int j = 7; int x = std::max(i,j);}
Will produce the following error with Visual Studio C++ 2005:
1>test.cpp(7) : error C2589: '(' : illegal token on right side of '::'1>test.cpp(7) : error C2143: syntax error : missing ';' before '::'
There are a number of ways to work around windows.h defining these two macros.
- Use alternative names defined in windows.h.
int x = _cpp_max(i,j);int y = _cpp_min(i,j);
This is not portable; only works on Windows.
- Define NOMINMAX before including windows.h. This might break existing code that assumes NOMINMAX is not defined.
- Don’t use std::min and std::max. Instead use the tertiary operator like so:
int x = i > j ? i : j; // max(i,j)int y = i < j ? i : j; // min(i,j)
This is portable but not as readable and more error prone.
- Use using statements to make the code portable:
using std::min;using std::max;int x = max(i,j);int y = min(i,j);
This works but requires two more lines of code. You could also just use ‘using namespace std;’ but that might pull in more than you want.
- Use std::min<int> and std::max<int>
int x = std::max<int>(i,j);int y = std::min<int>(i,j);
This requires you to specify the type. However in some cases this actually helps. For example:
int i = 5;unsigned int j = 7;int x = (std::max)(i,j);int y = (std::min)(i,j);
Note the ‘unsigned’. Generates the following errors:
1>test.cpp(7) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' :expects 3 arguments - 2 provided1> c:program filesmicrosoft visual studio 8vcincludexutility(3190) :see declaration of 'std::max'1>test.cpp(7) : error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' :template parameter '_Ty' is ambiguous1> c:program filesmicrosoft visual studio 8vcincludexutility(3182) :see declaration of 'std::max'1> could be 'unsigned int'1> or 'int'
By explicitly specifying type via <int> you remove the ambiguity.
- Use (std::min) and (std::max)
int i = 5;int j = 7;int x = (std::max)(i,j);int y = (std::min)(i,j);
This works (as does the std::max<int>) because the C++ preprocessor requires ‘(‘ as the next preprocessing token following the macro name to preform the macro expansion.
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max 出错解决方案
- std::min std::max
- 使用std:min(),std:max()
- std::max, std::min错误
- std::min and std::max与windows.h定义的min和max宏相冲突.
- std::min,std::max 与 MFC冲突的解决
- std::min&std::max与mfc冲突问题
- std::numeric_limits::max() std::numeric_limits::min()编译错误
- std::numeric_limits::max() std::numeric_limits::min()编译错误
- std::numeric_limits::max() std::numeric_limits::min()编译错误
- std::min, std::max, std::swap, std::rel_ops的简单使用
- 标准库std::min/std::max与min/max宏冲突
- std::max
- 最小路径覆盖问题求解及与最小边覆盖的区别
- easyui datagrid 日间格化(JS 日期时间本地化显示)
- Dedecms网站建设的基础知识
- Mac下配置环境变量不生效问题
- 数据科学家是做啥的?看看Stack Overflow这位大牛两年的经历就清楚了
- std::min and std::max
- 浅谈微服务与负载均衡LBS
- BZOJ 1799 [Ahoi2009]self 同类分布 数位DP
- easyui datagrid清空数据和去除表头
- 连接查询 子查询 SQL 查询 小记
- ldap
- maven-plugin集合
- AngularJS四大核心特性
- springmvc视图