在C99中,我包含了stdint.h,它给了我UINT32_MAX和uint32_t.但是,在C++中,UINT32_MAX被定义出来.我可以在包含stdint.h之前定义__STDC_LIMIT_MACROS,但是如果有人在已经包含stdint.h之后包含我的头文件,则这不起作用.
那么在C++中,找出uint32_t中可表示的最大值的标准方法是什么?
Gle*_*len 55
好吧,我不知道uint32_t但是对于基本类型(bool, char, signed char, unsigned char, wchar_t, short, unsigned short, int, unsigned int, long, unsigned long, float, double and long double)你应该使用numeric_limits模板#include <limits>.
cout << "Minimum value for int: " << numeric_limits<int>::min() << endl;
cout << "Maximum value for int: " << numeric_limits<int>::max() << endl;
Run Code Online (Sandbox Code Playgroud)
如果uint32_t是#define上述之一,则此代码应该开箱即用
cout << "Maximum value for uint32_t: " << numeric_limits<uint32_t>::max() << endl;
Run Code Online (Sandbox Code Playgroud)
Lio*_*gan 19
那么,uint32_t将始终为32位,并且始终是无符号的,因此您可以安全地手动定义它:
#define UINT32_MAX (0xffffffff)
Run Code Online (Sandbox Code Playgroud)
你也可以
#define UINT32_MAX ((uint32_t)-1)
Run Code Online (Sandbox Code Playgroud)