我想用QT4.7和MPIR库(v.2.3.1)在C++中编写程序。 在一些计算过程中,我需要存储动态数量的mpz_t(整数存储类型),并且希望使用QList或QVarLengthArray。我已经成功地做了一个关于如何做到这一点的基本测试,但看起来如此丑陋,显然是错误的,我想要求更好地做到这一点。将GMP/MPIR号码存储在QList中
我的示例程序:
#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。
请让我知道如何做到这一点:)
为什么你认为它不是安全的? – snoofkin 2011-05-22 08:37:04