如何在C ++中定义包含GMP变量的数组?

时间:2014-11-18 06:56:34

标签: c++ gmp

如何在C ++中创建一个包含mpz变量的数组?

我正在尝试使用:

int array_size = 5;
mpz_t numerator_arr;
for (i = 0; i < array_size; i++) {
    mpz_init2(numerator_arr[i], 100);
}
numerator_arr = { 1, -1, 1, 5, -691 };

但这会返回错误:

test.cpp: In function ‘int main()’:
test.cpp:13:34: error: cannot convert ‘__mpz_struct’ to ‘mpz_ptr {aka __mpz_struct*}’ for argument ‘1’ to ‘void __gmpz_init2(mpz_ptr, mp_bitcnt_t)’
   mpz_init2(numerator_arr[i], 100);
                                  ^
test.cpp:17:38: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
  numerator_arr = { 1, -1, 1, 5, -691 };
                                      ^
test.cpp:17:16: error: assigning to an array from an initializer list
  numerator_arr = { 1, -1, 1, 5, -691 };
                ^

编辑**:经过一些挖掘,我发现我需要用

声明我的数组
mpz_t *numerator_arr = new mpz_t[array_size];

但编译器仍然返回错误:

test.cpp:17:38: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
  numerator_arr = { 1, -1, 1, 5, -691 };
                                      ^
test.cpp:17:16: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘__mpz_struct (*)[1]’ in assignment
  numerator_arr = { 1, -1, 1, 5, -691 };
                ^

当我尝试使用

vector<mpz_class> numerator_arr{ 1, -1, 1, -1, 5, -691, 7 };

它给了我错误:

test.cpp:12:60: error: in C++98 ‘numerator_arr’ must be initialized by constructor, not by ‘{...}’
  vector<mpz_class> numerator_arr{ 1, -1, 1, -1, 5, -691, 7 };
                                                            ^
test.cpp:12:60: error: no matching function for call to ‘std::vector<__gmp_expr<__mpz_struct [1], __mpz_struct [1]> >::vector(<brace-enclosed initializer list>)’
test.cpp:12:60: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/vector:64:0,
                 from test.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/bits/stl_vector.h:398:9: note: template<class _InputIterator> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)
         vector(_InputIterator __first, _InputIterator __last,
         ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/bits/stl_vector.h:398:9: note:   template argument deduction/substitution failed:
test.cpp:12:60: note:   cannot convert ‘1’ (type ‘int’) to type ‘const allocator_type& {aka const std::allocator<__gmp_expr<__mpz_struct [1], __mpz_struct [1]> >&}’
  vector<mpz_class> numerator_arr{ 1, -1, 1, -1, 5, -691, 7 };
                                                            ^

当我尝试使用

mpz_class numerator_arr[] = { 1, -1, 1, -1 }

我试着存储像

这样的数字
-94598037819122125295227433069493721872702841533066936133385696204311395415197247711

编译器返回这些警告和错误:

test.cpp:29:3: warning: this decimal constant is unsigned only in ISO C90 [enabled by default]
test.cpp:30:4: warning: integer constant is too large for its type [enabled by default]
   -94598037819122125295227433069493721872702841533066936133385696204311395415197247711 };
    ^
test.cpp: In function ‘int main()’:
test.cpp:30:88: error: conversion from ‘__int128 unsigned’ to ‘mpz_class {aka __gmp_expr<__mpz_struct [1], __mpz_struct [1]>}’ is ambiguous
   -94598037819122125295227433069493721872702841533066936133385696204311395415197247711 };
                                                                                        ^
test.cpp:30:88: note: candidates are:
In file included from test.cpp:2:0:
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(double)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(float)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(long unsigned int)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(long int)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(short unsigned int)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(short int)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(unsigned int)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(int)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(unsigned char)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^
/usr/include/gmpxx.h:1423:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(signed char)
   __GMPXX_DEFINE_ARITHMETIC_CONSTRUCTORS
   ^

这是我第一次在GMP中使用数组。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,总结一下上面的所有注释,如果你在代码中处理很多数组修改(插入/删除),最好使用vector<mpz_class>来保存你的mpz_class变量。 使用mpz_class代替mpz_t更好的原因首先是您不必担心跟踪用于变量的内存(mpz_init()/mpz_clear())和第二,你编写的代码变得更加清晰,因为许多运算符都映射为mpz_class

关于错误,您的vector<mpz_class>代码导致错误,因为您没有正确实例化变量。可能是你的意思

mpz_class numerator_arr[] = { 1, -1, 1, -1 };
vector<mpz_class> arr (numerator_arr);

当你试图将大量数字存储到mpz_class中时,你会收到一个错误,因为你用这样的方式初始化mpz_class:

mpz_class m(-11111111111111111111111111111111111111111111111)

您已经将巨大的数字初始化为原始int类型。在你的情况下,数字被指定为无符号的16字节int因为它太大而gmp没有提供任何构造函数使其进入其mpz_class结构。 你最好将输入参数作为字符串(const char *)而不是int。所以它将是

mpz_class m("-11111111111111111111111111111111111111111111111")

希望这有帮助。