gpt4 book ai didi

c++ - 仅当某些值在 C++ 中保持不变时才读取某些列

转载 作者:行者123 更新时间:2023-11-30 04:09:51 39 4
gpt4 key购买 nike

我正在尝试从包含特定列数据的数据文件创建对象,前提是第一列的质量保持不变。这是我现在的尝试-

void read_mass() {
vector<double> value(29);
double _value ;
double _mass ;
double _next_mass ;
_file >> _mass ;
WimpData* mDM = new WimpData(_mass) ;
_next_mass = _mass ;
cout << "Reading data for mass " << _mass << endl;
do {
for (int i=0 ; i < 29 ; i++) {
_file >> _value ;
value[i]=_value;
cout << value[i] << " ";
}
mDM->add_line(value[0] ,value[23],value[24],value[25]);
if (_file.eof()) break ;
_file >> _next_mass ;
} while (_next_mass == _mass) ;
_wimp.insert(pair<double, WimpData*>(_mass, mDM));
cout << "Finished reading data for mass " << _mass << endl ;

}

我第一次使用此功能时,它可以正常工作。在第二次调用中,我看到文件的指针并没有停留在质量有新值的位置,而是只走了一步。

我怎样才能使文件的指针在 do-while 循环中继续计数?

最佳答案

问题是因为您读取了 _next_mass 并且在下一次执行中您从第一个值开始读取到 _mass(您之前在执行中读取过它)。

例如,您可以为读取它做类,并将最后读取的质量保存在私有(private)变量中。并添加将读取初始质量的任何函数 init()。

快速解决方案:

double _mass = -1.0; //initial mass firstly not read (-1)
void read_mass() {
vector<double> value(29);
double _value ;
double _next_mass ;
if(_mass == -1)
_file >> _mass ;
WimpData* mDM = new WimpData(_mass) ;
_next_mass = _mass ;
cout << "Reading data for mass " << _mass << endl;
do {
for (int i=0 ; i < 29 ; i++) {
_file >> _value ;
value[i]=_value;
cout << value[i] << " ";
}
mDM->add_line(value[0] ,value[23],value[24],value[25]);
if (_file.eof()) break ;
_file >> _next_mass ;
} while (_next_mass == _mass) ;
_wimp.insert(pair<double, WimpData*>(_mass, mDM));
cout << "Finished reading data for mass " << _mass << endl ;


_mass = _next_mass; //we readed the first mass of next line (save it)
}

关于c++ - 仅当某些值在 C++ 中保持不变时才读取某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20923981/

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