gpt4 book ai didi

c++ - 从文件读取到 vector - 最后一行重复

转载 作者:行者123 更新时间:2023-11-30 01:57:49 27 4
gpt4 key购买 nike

<分区>

我正在尝试将值从文件读取到 vector

std::vector<float> setTimesArray (std::string flName){
int i=0, dummy=0;
float temp;
std::vector<float> pObs;
std::string line;
std::ifstream inFile;
inFile.open(flName.c_str());
if(!inFile){
std::cout<<"\n.obs file not valid. Quitting programme...";
exit(1);
}
while(inFile.good()){
i++;
getline(inFile, line);
if(i>=3){ //I don't want first two lines
std::istringstream in(line);
in>>dummy;//discards first value in the line
in>>temp;
pObs.push_back(temp);
in.str(""); //discards remaining part of the line
}

}
return pObs;
inFile.close();
}

问题是,最后一个值会重复。例如,flName 共有 975 行。因此 pObs 必须具有 size=973(975-2 初始行)。但是大小是 974,我看到最后一个值是重复的。我犯了什么错误?

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