快速解析c++中以制表符分隔的字符串和整型
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
}
有那么慢吗?
相关文章:
- 如何分隔字符串并将标记传递给方法
- C++ 用空格和逗号分隔字符串
- 如何将整数值解析为逗号分隔字符串中的单独 int 变量?
- 如何分隔字符串并将每个部分存储在不同的向量中
- 如何按特定分隔符分隔字符串
- 逗号分隔字符串的总和,例如 O(n) 中的 4,1,3
- 如何使用符号分隔字符串而不是空格的输入字符串流
- C++中将不同类型的变量列表连接到逗号分隔字符串的最快方法是什么?
- C++ sscanf 空格分隔字符串
- 在C++中分隔字符串并存储到数组中的最佳方法
- 使用 find_first_of() 和 substr() 分隔字符串,但我得到的输出不正确
- 正在分析逗号分隔字符串中的整数
- 分析分隔字符串
- 用空格分隔字符串的最简单方法
- 分隔字符串的最佳ASCII字符是什么
- C++用空格和标点符号分隔字符串
- c#中分隔字符串中的数字和单位
- 如何基于标记分隔字符串
- c++使用wstring拆分unicode分隔字符串
- 如何在C++中用逗号分隔字符串