C++:最大整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1732011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: max integer
提问by sivabudh
Is there a C++ cross-platform library that provides me with a portable maximum integer number?
是否有 C++ 跨平台库可以为我提供可移植的最大整数?
I want to declare:
我想声明:
const int MAX_NUM = /* call some library here */;
I use MSVC 2008 unmanaged.
我使用 MSVC 2008 非托管。
回答by James McNellis
In the C++ standard library header <limits>
, you will find:
在 C++ 标准库头文件中<limits>
,您会发现:
std::numeric_limits<int>::max()
Which will tell you the maximum value that can be stored in a variable of type int
. numeric_limits
is a class template, and you can pass it any of the numeric types to get the maximum value that they can hold.
这将告诉您可以存储在类型变量中的最大值int
。 numeric_limits
是一个类模板,您可以将任何数字类型传递给它以获得它们可以容纳的最大值。
The numeric_limits
class template has a lot of other information about numeric types as well.
在numeric_limits
类模板有很多的有关数值类型的其他信息,以及。
回答by Luká? Lalinsky
See limits.h
(C) or climits
(C++). In this case you would want the INT_MAX
constant.
参见limits.h
(C) 或climits
(C++)。在这种情况下,您需要INT_MAX
常量。
回答by MJeB
I know it's an old question but maybe someone can use this solution:
我知道这是一个老问题,但也许有人可以使用这个解决方案:
int size = 0; // Fill all bits with zero (0)
size = ~size; // Negate all bits, thus all bits are set to one (1)
So far we have -1 as result 'till size is a signed int.
到目前为止,我们有 -1 作为结果,直到 size 是一个有符号整数。
size = (unsigned int)size >> 1; // Shift the bits of size one position to the right.
As Standard says, bits that are shifted in are 1 if variable is signed and negative and 0 if variable would be unsigned or signed and positive.
正如标准所说,如果变量有符号且为负,则移入的位为 1,如果变量为无符号或有符号且为正,则移入的位为 0。
As size is signed and negativ we would shift in sign bit which is 1, which is not helping much, so we cast to unsigned int, forcing to shift in 0 instead, setting the sign bit to 0 while letting all other bits remain 1.
由于大小是有符号和负数,我们将移入符号位 1,这没有多大帮助,因此我们转换为 unsigned int,强制移入 0,将符号位设置为 0,同时让所有其他位保持为 1。
cout << size << endl; // Prints out size which is now set to maximum positive value.
We could also use a mask and xor but then we had to know the exact bitsize of the variable. With shifting in bits front, we don't have to know at any time how many bits the int has on machine or compiler nor need we include extra libraries.
我们也可以使用掩码和异或,但是我们必须知道变量的确切位大小。通过在位前移位,我们不必随时知道机器或编译器上 int 有多少位,也不需要包含额外的库。
回答by Mike Gleason jr Couturier
I know the answer has been given but I just want to know from my old days, I used to do
我知道答案已经给出,但我只想知道从我过去的日子,我曾经做过
int max = (unsigned int)-1
Will it give the same as
它会给出与
std::numeric_limits<int>::max()
?
?
回答by yet
On Hp UX with aCC compiler:
在带有 aCC 编译器的 Hp UX 上:
#include <iostream>
#include <limits>
using namespace std;
int main () {
if (sizeof(int)==sizeof(long)){
cout<<"sizeof int == sizeof long"<<endl;
} else {
cout<<"sizeof int != sizeof long"<<endl;
}
if (numeric_limits<int>::max()==numeric_limits<long>::max()){
cout<<"INT_MAX == lONG_MAX"<<endl;
} else {
cout<<"INT_MAX != LONG_MAX"<<endl;
}
cout << "Maximum value for int: " << numeric_limits<int>::max() << endl;
cout << "Maximum value for long: " << numeric_limits<long>::max() << endl;
return 0;
}
It prints:
它打印:
sizeof int == sizeof long
sizeof int == sizeof long
INT_MAX != LONG_MAX
INT_MAX != LONG_MAX
I checked both int and long types are 4bytes. manpage limits(5) says that INT_MAX and LONG_MAX are both 2147483647
我检查了 int 和 long 类型都是 4bytes。联机帮助页限制(5) 说 INT_MAX 和 LONG_MAX 都是 2147483647
http://nixdoc.net/man-pages/HP-UX/man5/limits.5.html
http://nixdoc.net/man-pages/HP-UX/man5/limits.5.html
So, conclusion std::numeric_limits< type >:: is not portable.
因此,结论 std::numeric_limits< type >:: 不可移植。