std::min and std::max
来源:互联网 发布:经常性失眠的原因 知乎 编辑:程序博客网 时间:2024/06/01 08:36
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:
之所以为std::max添加括号, 是因为在windows.h里面定义了"max"和"min"的宏。如果你的程序中包含了windows.h如下所示:
#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:
在VS05(及其他的版本)中就会出现这样的错误,
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.
有多种该方法可以使用windows.h里面定义的这两个宏.
- Use alternative names defined in windows.h.
- 1 使用这两个函数在windows.h里面定义的别名
int x = _cpp_max(i,j);int y = _cpp_min(i,j);
This is not portable; only works on Windows.
这是不能跨平台的,只能在windows里面使用.
- Define NOMINMAX before including windows.h. This might break existing code that assumes NOMINMAX is not defined.
- 在使用windows.h之前定义 "NOMINMAX" 宏,但是,这将导致已经以未定义此宏为前提而使用的函数出错.
- Don’t use std::min and std::max. Instead use the tertiary operator like so:
- 不再使用std::min,std::max , 使用如下的三目运算符:
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声明
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.
这虽可行但是多写了两行代码, 我们也可以添加using namespace std; 但这样,会出现其他潜在的问题, 如打破命名空间.
- Use std::min<int> and std::max<int>
- 使用std::min<int> 和 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:(注意,"unsingned"如引发如下的错误)
1>test.cpp(7) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' :expects 3 arguments - 2 provided(提供的参数过个数不正确)1> 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
- 欢迎使用CSDN-markdown编辑器
- Apache Rewrite配置
- 权限冲突无法安装
- centos min wget的使用
- 性能测试基本指标浅谈
- std::min and std::max
- yarn 日志聚集配置
- Oracle索引的介绍和使用
- 大型网站架构系列:负载均衡详解(1)
- Web App自适应布局
- 高通平台手机开发之TP
- Java中字符串倒序方法
- swift 枚举
- el表达式里面fn的用法