可能是对不起:/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <boost/algorithm/string.hpp>
using namespace std;
class CSVReader{
string fileName;
string delimeter;
public:
CSVReader(string filename, string delm = ",") :
fileName(filename), delimeter(delm)
{ }
vector<vector<string> > getData();
};
vector<vector<string> > CSVReader::getData(){
ifstream file(fileName);
vector<vector<string> > dataList;
string line = "";
while (getline(file, line)){
vector<string> vec;
boost::algorithm::split(vec, line, boost::is_any_of(delimeter));
dataList.push_back(vec);
}
file.close();
return dataList;
}
int main(){
int counter=-1;
CSVReader reader("ncov_confirmed.csv");
vector<vector<string> > confirmed = reader.getData();
reader = CSVReader("ncov_deaths.csv");
vector<vector<string> > deaths = reader.getData();
reader = CSVReader("ncov_recovered.csv");
vector<vector<string> > recovered = reader.getData();
vector<vector<string> > output;
output.resize(2592);
for (auto &inner : output){
inner.resize(6);
}
for(int j = 0; j<36; j++){
for(int i = 4; i<40; i++){
for(int k = 0; k<72; k++){
counter++;
output[counter][0]=confirmed[0][i];
cout << counter << "\t" << output[counter][0] << "\t" << confirmed [0][i] << endl;
}
}
}
cout << "end";
}
以下循环后出现分段错误。当cout <<“ end”时,我注意到了这个问题;不输出任何东西。它输出我在for循环中编程的内容,然后输出动臂。细分错误。
for(int j = 0; j<36; j++){
for(int i = 4; i<40; i++){
for(int k = 0; k<72; k++){
counter++;
output[counter][0]=confirmed[0][i];
cout << counter << "\t" << output[counter][0] << "\t" << confirmed [0][i] << endl;
}
}
}
有人可以向我解释为什么存在细分错误,如何解决吗?任何帮助表示赞赏。附言第一次编译时,我得到随机分段错误代码,它给出了82302,然后是82334,现在是82557。为什么?
答案 0 :(得分:0)
很高兴,我发现了自己的错误。我创建了一个矢量输出[2592] [6],我的计数器达到103680。就像我将2592个苹果放入一个盒子,现在我正试图找到第103680个苹果。不可能吧?因此正确的代码应为:
for(int i = 4; i<40; i++){
for(int k = 0; k<72; k++){
counter++;
output[counter][0]=confirmed[0][i];
//cout << counter << "\t" << output[counter][0] << "\t" << confirmed [0][i] << endl;
}
}