c++有效地跳过多行
C++ skip multiple lines efficiently
我有代码读取每四行,并对它做一些事情
ifstream in(inputFile, ios::in);
string zeile;
for (int z = 0; z < numberOfSequences; z++) {
getline(in,zeile); // skip 3 lines
getline(in,zeile); // skip 3 lines
getline(in,zeile); // skip 3 lines
getline(in,zeile);
// do something with zeile
}
我的问题是,ASCII文件有超过250 000 000行。所以我感兴趣的是跳过3行的最有效方法。getline
是否将in
解析为字符串,或者这是最有效的方法?我不想浪费时间在跳绳上。
这几乎是最有效的方法;唯一发生的"解析"是搜索您确实需要的行尾。
你唯一可以改进的是不要不必要地存储比你实际要处理的多四倍的行。你可以用std::basic_istream::ignore
:
std::ifstream in(inputFile, std::ios::in);
for (int z = 0; in && z < numberOfSequences; z++) {
// Skip three lines
for (int i = 0; i < 3; i++)
in.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
// Read the fourth line...
std::string zeile;
if (std::getline(in, zeile))
foo(zeile);
}
相关文章:
- 有效地使用std::unordered_map来插入或增加键的值
- 如何有效地在 std::vector 中插入一对?
- 有效地计算多维数组的累积和?
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 有效地将大数存储为 2 的幂用于路径问题
- 如何在C++中写入 1000 个文件时有效地缓冲
- 如何有效地找到数组中三元组和的最小差异?
- 如何在C++中有效地将数字值重新分配给字符数组
- C++有效地找到向量中第一个最接近的匹配值?
- 如何有效地操作满足给定谓词的向量中的所有项目?
- 有效地将数据加载到 std::vector 中<char>
- 如何在使用 cin 请求 int 时有效地使用户输入万无一失?
- C++:有效地将Sha256摘要放入OpenSSL Bignum?
- 如何有效地收集给定数组中的重复元素?
- 如何有效地修剪和合并四叉树中的节点?
- 可以有效地转换 std::any 与 std::any_cast
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 当表示为对象的一维向量时,有效地旋转 NxM 矩阵 (C++)
- 我们怎样才能有效地压缩DNA串