C++ 中是否有比 long long int 更大的类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5381882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:02:18  来源:igfitidea点击:

Are there types bigger than long long int in C++?

c++types

提问by Michael

Are there types bigger than long long int in C++?

C++ 中是否有比 long long int 更大的类型?

My compiler is g++.

我的编译器是 g++。

回答by Jon

回答by KitsuneYMG

__int128
__uint128

__int128
__uint128

????? ???????????????

????? ???????????????

回答by jho

No, but you can use libraries like GMPto handle bigger numbers.

不,但您可以使用GMP 之类的库来处理更大的数字。

回答by J T

Depending on what your need is, you could create your own struct to handle the data type:

根据您的需要,您可以创建自己的结构来处理数据类型:

#include <cstdint>

struct uint256_t
{
    std::uint64_t bits[4];
};

uint256_t x;

回答by Spike0xff

Summarizing...

总结...

If you need to store exact integer values that won't fit in 'long long', gcc offers the type __int128. This is a gcc extension, not part of standard C++ (as of this writing).

如果您需要存储不适合 'long long' 的精确整数值,gcc 提供__int128类型。这是一个 gcc 扩展,不是标准 C++ 的一部分(在撰写本文时)。

If you need to work with even bigger exact integer values, you probably need an arbitrary-precision arithmetic package, such as GMP. If your need is very limited you could roll your own extended precision code, but that can quickly become more complicated (and less efficient and reliable) than using an existing library.

如果您需要处理更大的精确整数值,您可能需要一个任意精度的算术包,例如GMP。如果您的需求非常有限,您可以推出自己的扩展精度代码,但与使用现有库相比,这会很快变得更加复杂(并且效率和可靠性较低)。

If you need to store larger numbers but don't need to store the larger values exactly, you can use floator double: These can represent numbers of much larger magnitude, but with less precision.

如果您需要存储更大的数字但不需要精确存储更大的值,您可以使用floatdouble:它们可以表示更大数量级但精度较低的数字。

And of course, if you just want to take up more memory, declare an array ;-)

当然,如果您只想占用更多内存,请声明一个数组;-)

回答by Gunner Stone

If you know your number is always going to be positive, you can extend the scope of an intby labeling it as unsigned

如果您知道您的数字始终为正数,则可以int通过将其标记为unsigned

int myNum; // Range is from –2,147,483,648 to 2,147,483,647

int myNum; // Range is from –2,147,483,648 to 2,147,483,647

unsigned int myNum; // Range is from 0 to 4,294,967,295

unsigned int myNum; // Range is from 0 to 4,294,967,295

回答by Kapil

You can use

您可以使用

#include <boost/multiprecision/cpp_int.hpp>  
using namespace boost::multiprecision;

to work with data type bigger than long long int and the data type is cpp_intRef

使用大于 long long int 的数据类型,数据类型为cpp_intRef

回答by TCS

you can check out BigInt class... http://sourceforge.net/projects/cpp-bigint/

您可以查看 BigInt 类... http://sourceforge.net/projects/cpp-bigint/

(There are many other BigInts out there...)

(还有很多其他的 BigInts ......)