我正在开发一个 C++ 程序(C++ 98)。它读取一个包含很多行(10000 行)的文本文件。这些是制表符分隔的值,然后我将其解析为 Vector 对象的 Vector。但是它似乎适用于某些文件(较小),但我的一个文件给了我以下错误(这个文件有 10000 行,它是 90MB)。我猜这是与内存相关的问题?你能帮我么?
错误
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Abort
代码
void AppManager::go(string customerFile) {
vector<vector<string> > vals = fileReader(customerFile);
for (unsigned int i = 0; i < vals.size();i++){
cout << "New One\n\n";
for (unsigned int j = 0; j < vals[i].size(); j++){
cout << vals[i][j] << endl;
}
cout << "End New One\n\n";
}
}
vector<vector<string> > AppManager::fileReader(string fileName) {
string line;
vector<vector<string> > values;
ifstream inputFile(fileName.c_str());
if (inputFile.is_open()) {
while (getline(inputFile,line)) {
std::istringstream iss(line);
std::string val;
vector<string> tmp;
while(std::getline(iss, val, '\t')) {
tmp.push_back(val);
}
values.push_back(tmp);
}
inputFile.close();
}
else {
throw string("Error reading the file '" + fileName + "'");
}
return values;
}