超过 C++ 的最大整数数据类型

Exceeding C++'s largest integer datatype

本文关键字:整数 数据类型 C++ 超过      更新时间:2023-10-16

我正在编写一个组合计算器,对于较大的计算,我最终会遇到long long intint64_t溢出。至少有可能把这个数字转换成这样的数字吗:6.7090373691429E+19?

这是我的代码:

#include <iostream>
#include <string.h>
#include <math.h>
int main() {

  std::string charset;
  int i, length; int64_t total = 0;
  std::cout << "Charset: ";
  std::cin >> charset;
  std::cout << "Length: ";
  std::cin >> length;
    for (i=0;i<(length+1);i++) {
        total += pow(charset.size(),i);
    }
    std::cout << "nPossible combinations: " << total << std::endl;
    return 0;
}

C++标准库不包括任意大小的整数类型。

您可以使用Boost Multiprecision。它有不同的后端,使用专用库(如GMP(和没有外部依赖关系的自定义后端(cpp_int(。

编辑:公平地说,微软已经在评论中提到了Boost Multiprecision。