我試圖編寫一個讀取CSV文件的程序(無需擔心轉義任何東西,它嚴格格式化爲不帶引號),但值爲0的任何數字項目是而只是留空。因此法線會是什麼樣子:使用可選列高效地讀取CSV文件
12,STRING1,string2,3 ,,, string3,4.5
,而不是
12,STRING1,string2,3,0,0,string3,4.5
我有一些使用矢量的工作代碼,但速度太慢。
int main(int argc, char** argv)
{
string filename("path\\to\\file.csv");
string outname("path\\to\\outfile.csv");
ifstream infile(filename.c_str());
if(!infile)
{
cerr << "Couldn't open file " << filename.c_str();
return 1;
}
vector<vector<string>> records;
string line;
while(getline(infile, line))
{
vector<string> row;
string item;
istringstream ss(line);
while(getline(ss, item, ','))
{
row.push_back(item);
}
records.push_back(row);
}
return 0;
}
是否有可能超載運營商類似How to use C++ to read in a .csv file and output in another form?的ostream的< <時域可以是空白的? 會提高性能嗎?
或者還有什麼我能做的,讓這跑得更快? 謝謝
您應該首先分析您的程序,以確定哪些部件使其運行緩慢。 –