The min/max problem in C++ and Windows

本文探讨了Windows头文件中定义的min/max宏可能与C++标准库中的std::min()/std::max()函数引发的冲突及错误问题,并提供了多种解决方案以避免此类问题。

cite from http://www.suodenjoki.dk/us/archive/2010/min-max.htm

I recently discovered that a Windows header file - specifically the WinDef.h - defines two macros min and max which may result in conflicts and compiler errors.

You'll hit compiler errors such as:

error C2589: '(' : illegal token on right side of '::'

Any C++ source code including the Windows header will likely head into problems if min or max are used together with the Standard C++ library functions std::min()/std::max() as defined in <algorithm>.

The issue is that using a macro definition for such common names is a bad idea and the min/max problem itself has been there for several years (a Google search can confirm that). So the Windows header should never had defined these macros.

Note: It beats me why Microsoft haven't changed WinDef.h years ago. Also as the min/max are macros they should have been more clearly marked as such - using the common syntactic practice of using MIN/MAX in uppercase.

Note: You should note that Microsoft also have the __min()/__max() C functions as declared in Visual C++'s <stdlib.h> (for C old-stylers) or <cstdlib> (For C++ coolers). These can be used as alternatives, but you should really prefer using std::min() and std::max() because these are part of the C++ standard. Remember that Microsoft prefixes non standard structures and functions with underscore '_' character.

Solution

The solution is one or several of the following (prioritized with best approach first):

  1. Convert your source code to use the Standard C++ std::min() and std::max() and ensure that you #include <algorithm>. Use
    1 #include <algorithm>
    2 using namespace std;
    3  
    4 // use min() instead of std::min()
    5 // use max() instead of std::max()
  2. Define the NOMINMAX macro to instruct WinDef.h not to define the min/max macros. E.g. you can update your Visual C++ compiler options with /D NOMINMAX or you can insert #define NOMINMAX.
  3. Redefine min/max in the specific problematic files. Especially this may be needed in you include gdiplus Windows headers files, because these uses the Windows min/max macros.
    01 #include <algorithm>
    02  
    03 #ifndef max
    04 #define max(a,b) (((a) > (b)) ? (a) : (b))
    05 #endif
    06 #ifndef min
    07 #define min(a,b) (((a) < (b)) ? (a) : (b))
    08 #endif
    09  
    10 #include <gdiplus.h>
    11 #undef max
    12 #undef min
    13  
    14 using namespace std;
  4. Alternatively redefine min/max to use use the Visual C++ non-standard __min()/__max(). But min/max are still defined as macros and will likely lead to problems. Not a good solution.
    1 #ifndef max
    2 #define max __max
    3 #endif
    4 #ifndef min
    5 #define min __min
    6 #endif
  5. Alternatively use Visual C++ compiler options /Dmin=__min / Dmax=__max. This will tell compiler and WinDef.h not to define min/max macros and use __min() and __max() functions instead as defined in <cstdlib> (or <stdlib.h>) so ensure this is included. But min/max are still defined as macros and will likely lead to problems. Not a good solution.

More (updated November 26th 2013):

Problem Statement Let N be a positive integer. Define the imbalance of a sequence A=(A 1 ​ ,A 2 ​ ,…,A 2 N ​ ) of non-negative integers of length 2 N as the non-negative integer value obtained by the following operation: Initially, set X=0. Perform the following series of operations N times: Update X to max(X,max(A)&minus;min(A)), where max(A) and min(A) denote the maximum and minimum values of sequence A, respectively. Form a new sequence of half the length by pairing elements from the beginning two by two and arranging their sums. That is, set A←(A 1 ​ +A 2 ​ ,A 3 ​ +A 4 ​ ,…,A ∣A∣&minus;1 ​ +A ∣A∣ ​ ). The final value of X is the imbalance. For example, when N=2,A=(6,8,3,5), the imbalance is 6 through the following steps: Initially, X=0. The first series of operations is as follows: Update X to max(X,max(A)&minus;min(A))=max(0,8&minus;3)=5. Set A to (6+8,3+5)=(14,8). The second series of operations is as follows: Update X to max(X,max(A)&minus;min(A))=max(5,14&minus;8)=6. Set A to (14+8)=(22). Finally, X=6. You are given a non-negative integer K. Among all sequences of non-negative integers of length 2 N with sum K, construct a sequence that minimizes the imbalance. Constraints 1≤N≤20 0≤K≤10 9 N and K are integers. Input The input is given from Standard Input in the following format: N K Output Let B=(B 1 ​ ,B 2 ​ ,…,B 2 N ​ ) be a sequence with minimum imbalance. Let U be the imbalance of B. Output a solution in the following format: U B 1 ​ B 2 ​ … B 2 N ​ If there are multiple solutions, any of them will be considered correct. Sample Input 1 Copy 1 11 Sample Output 1 Copy 1 5 6 (5,6) is a sequence with imbalance 1, which is the minimum imbalance among sequences satisfying the condition. Sample Input 2 Copy 3 56 Sample Output 2 Copy 0 7 7 7 7 7 7 7 7 使用c++代码,尽量少有空格,简化变量名,不使用vector,下标从1开始,添加行首缩进
最新发布
09-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值