我有一个简单的方法,即在向量中搜索精确值(从文件中获取的数据)或插入不存在的一次。我之前已经多次运行过这个程序而且它正在工作,但突然之间却没有。 代码:
double Integr::F(double x_val, double q2_val){
int i=0;
bool interpolate_x=0, interpolate_q=0;
vector<double>::iterator it = begin(x_f);
for(; it != end(x_f) ; it += 201){
if(*it == x_val){
interpolate_x = 0;
break;
}
else if(*it > x_val){
interpolate_x = 1;
break;
}
i += 201;
}
it = begin(q2)+i;
for(; it != end(q2) ; ++it){
if(*it == q2_val){
interpolate_q = 0;
break;
}
else if(*it > q2_val){
interpolate_q = 1;
break;
}
++i;
}
double f1, f2;
if(interpolate_x){
f1 = Interpolate::interpolate(f[i], f[i-201], x_f[i], x_f[i-201], x_val);
f2 = Interpolate::interpolate(f[i-1], f[i-202], x_f[i-1], x_f[i-202], x_val);
if(!interpolate_q) return f1;
else{
return Interpolate::interpolate(f1, f2, q2[i], q2[i-1], q2_val);
}
}
if(interpolate_q){
return Interpolate::interpolate(f[i], f[i-1], q2[i], q2[i-1], q2_val);
}
else return f[i];
}
首先,valgrind表示:
==4138== Invalid read of size 8
==4138== at 0x4021C8: Integr::F(double, double) (Integr.cpp:81)
它指向上述方法的最后一行:
else return f[i];
谁能告诉我出了什么问题?
[编辑] 从文件中读取的方法:
void Data::read_File() {
if(file.good() == true){
double tmp1, tmp2, tmp3;
int i=0;
while(!file.eof()) {
++i;
file >> tmp1;
file >> tmp2;
file >> tmp3;
x.push_back(tmp1);
q.push_back(tmp2);
f.push_back(tmp3);
cout << i << endl;
}
file.close();
} else cout << "Access denied! \n";
}