gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:51:56 26 4
gpt4 key购买 nike

如何在 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 中使用数组。任何帮助将不胜感激。

最佳答案

好吧,总结上面的所有评论,如果您在代码中处理大量数组修改(插入/删除),最好使用 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)

您已经将 huge number 初始化为原始 int 类型。在你的例子中,这个数字被分配为一个无符号的 16 字节 int,因为它太大了,gmp 没有提供任何构造函数来将它变成它的 mpz_class。结构。您最好将输入参数设为字符串(const char*) 而不是 int。所以会是

mpz_class m("-11111111111111111111111111111111111111111111111")

希望这对您有所帮助。

关于c++ - 如何在 C++ 中定义一个包含 GMP 变量的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26988385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com