我正在尝试使用 QT4.7 和 MPIR 库(v. 2.3.1)用 C++ 编写程序。在某些计算过程中,我需要存储动态数量的 mpz_t(整数存储类型),并希望为此使用 QList 或 QVarLengthArray。我已经成功地设置了一个关于如何做到这一点的基本测试,但它看起来如此丑陋和完全错误,我想要求一个更好的方法来做到这一点。
我的示例程序:
#include <QtCore/QCoreApplication>
#include <QList>
#include <qtimer.h>
#include <mpirxx.h>
#include <iostream>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QList<__mpz_struct> test;
std::cout << "Write ints 0 to 9 in the QList" << std::endl;
for (int i = 0; i < 10; ++i) {
mpz_t zahl;
mpz_init_set_si(zahl, i);
test.append(zahl[0]);
}
std::cout << "Check if everything is still there." << std::endl;
for (int i = 0; i < 10; ++i) {
mpz_t zahl;
zahl[0] = test.at(i);
std::cout << mpz_get_str(NULL, 10, zahl) << std::endl;
}
std::cout << "What an ugly hack." << std::endl;
QTimer::singleShot(0, &a, SLOT(quit()));
return a.exec();
}
(在Windows 7/MSVC2010 SP1/QT4.7.3/MPIR2.3.1下编译)输出是正确的,但我怀疑它是一种有效甚至安全的存储mpz_t的方式。
请让我知道如何实现这一目标:)