头文件
#include <vector>
#include <QDebug>
#include<qmath.h>
#include <QTime>
using namespace std;
class Vector_test
{
public:
Vector_test();
double rand();
};
源文件
Vector_test::Vector_test()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
vector<double> arrayTest(10);
qDebug() << arrayTest;
for (int i = 0; i < 10; ++i) {
arrayTest[i] = rand();
}
qDebug() << arrayTest;
}
double Vector_test::rand()
{
return (qrand()%10)/10.0+(qrand()%10)/100.0+(qrand()%10)/1000.0;
}
结果
std::vector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
std::vector(0.163, 0.111, 0.24, 0.007, 0.046, 0.734, 0.936, 0.885, 0.76, 0.425)
除法运算/ 和求余%
对于int类型的函数,必须要有返回值return X,但是不影响qDebug() 和 cout(), 最后一行写一个return 0就可以。
int Test::Out()
{
int b = 41;
int a = (41%10);
double aa = (41%10)/10.0;
qDebug() << b << a << aa;
return 0;
}
41 1 0.1