Where are std::min() and std::max()?
The Standard Library defines the two template functions std::min() and std::max() in the <algorithm> header. In general, you should use these template functions for calculating the min and max values of a pair. Unfortunately, Visual C++ does not define these function templates. This is because the names min and max clash with the traditional min and max macros defined in <windows.h>. As a workaround, Visual C++ defines two alternative templates with identical functionality called _cpp_min() and _cpp_max(). You can use them instead of std::min() and std::max().To disable the generation of the min and max macros in Visual C++, #define NOMINMAX before #including <windows.h>.
标准库在<algorithm>头中定义了两个模板函数std::min() 和 std::max()。通常用它可以计算某个值对的最小值和最大值。可惜在 Visual C++ 无法使用它们,因为没有定义这些函数模板。原因是名字min和max与<windows.h>中传统的min/max宏定义有冲突。为了解决这个问题,Visual C++ 定义了另外两个功能相同的模板:_cpp_min() 和 _cpp_max()。我们可以用它们来代替std::min() 和 std::max()。为了禁用Visual C++中的 min/max宏定义,可以在包含<windows.h>头文件之前加上:
#define NOMINMAX
或者将 NOMINMAX 加到 preprocessor difinitions 中。具体方法是:
Visual C++ 6.0: 打开工程文件(*.dsw)或者(*.dsp),选中要定义的工程,进入菜单“Project”=>“Settings”(打开Settings对话框)=> 在左上角的“Setting for”下拉列表框处选择“All configurations”,然后选中“C/C++”标签=>在 “preprocessor difinitions”编辑框中加上“NOMINMAX”即可。
Visual C++ 7.0: 打开工程项目文件(*.sln)或者(*.vcproj),选中要定义的工程项目,进入菜单“项目”=>“属性”(打开项目属性对话框)=>在左上角的“配置”下拉列表框处选择“所有配置”,然后在左边的“属性配置”中选择=>“C/C++”=>“预处理器”=>右边“预处理器定义”处加上“NOMINMAX”。
#define NOMINMAX
或者将 NOMINMAX 加到 preprocessor difinitions 中。具体方法是:
Visual C++ 6.0: 打开工程文件(*.dsw)或者(*.dsp),选中要定义的工程,进入菜单“Project”=>“Settings”(打开Settings对话框)=> 在左上角的“Setting for”下拉列表框处选择“All configurations”,然后选中“C/C++”标签=>在 “preprocessor difinitions”编辑框中加上“NOMINMAX”即可。
Visual C++ 7.0: 打开工程项目文件(*.sln)或者(*.vcproj),选中要定义的工程项目,进入菜单“项目”=>“属性”(打开项目属性对话框)=>在左上角的“配置”下拉列表框处选择“所有配置”,然后在左边的“属性配置”中选择=>“C/C++”=>“预处理器”=>右边“预处理器定义”处加上“NOMINMAX”。