[英]Quickly parse tab-separated strings and ints in c++
我有一個文件,大小為幾GB,並且有數百萬行。 每行都有分離的數據,如下所示:
string TAB int TAB int TAB int NEWLINE
我之前嘗試逐行讀取此文件的原因是CPU瓶頸,而不是SSD的寫入速度。
如何快速逐行解析大量文件?
注意:由於文件太大,因此無法一次全部解析為向量。
在我的原始代碼中,我將數據解析為這樣的結構向量
struct datastruct {
std::string name;
int year;
int occurences;
int volcount;
};
std::vector<datastruct> data;
使用您的datastruct
,您可以做
std::ifstream file;
datastruct data;
while (file >> data.name >> data.year >> data.occurences >> data.volcount)
{
// do what you want with data, its contents will be replaced during next iteration
}
那慢嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.