作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从包含特定列数据的数据文件创建对象,前提是第一列的质量保持不变。这是我现在的尝试-
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/
我是一名优秀的程序员,十分优秀!