GMP:将整数转换为std :: string

问题描述 投票:0回答:2

GMP是否具有将std字符串转换为整数的功能?

函数mpz_init_set_str初始化并将char *转换为int。我想知道有没有对std字符串的支持?

c++ stdstring gmp
2个回答
3
投票

只需使用c_str()函数访问底层char数组:

std::string str;
mpz_t strg;
mpz_init_set_str(strg, str.c_str(), 10);

0
投票

GMP具有C ++绑定,因此使用gmpxx并且它可以正常工作。简单的任务将完成工作(所以没有锅炉板代码)。

Even introduction展示了这样的例子:

int
main (void)
{
  mpz_class a, b, c;

  a = 1234;
  b = "-5678";
  c = a+b;
  cout << "sum is " << c << "\n";
  cout << "absolute value is " << abs(c) << "\n";

  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.