快速解析c++中以制表符分隔的字符串和整型

Quickly parse tab-separated strings and ints in c++

本文关键字:分隔 字符串 整型 制表符 c++      更新时间:2023-10-16

我有一个几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
}

有那么慢吗?