跳过std :: istream中的行 [英] Skip lines in std::istream

查看:151
本文介绍了跳过std :: istream中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用std :: getline()从std :: istream派生类读取行,我如何前进几行?

I'm using std::getline() to read lines from an std::istream-derived class, how can I move forward a few lines?

推荐答案

编辑:您也可以使用std: :istream :: ignore,请参阅 http://stackoverflow.com/a/25012566/492336

You can also use std::istream::ignore, see http://stackoverflow.com/a/25012566/492336


我必须使用getline我想跳过的行数吗?

Do I have to use getline the number of lines I want to skip?

不,但它可能是读取代码的最清晰的解决方案。如果您跳过的行数很大,您可以通过读取大块和计算每个块中的换行符,停止并将文件重新定位到最后一个换行符的位置来提高性能。但是除非你有性能问题,我只是把getline放在你想跳过的行数的循环中。

No, but it's probably going to be the clearest solution to those reading your code. If the number of lines you're skipping is large, you can improve performance by reading large blocks and counting newlines in each block, stopping and repositioning the file to the last newline's location. But unless you are having performance problems, I'd just put getline in a loop for the number of lines you want to skip.

这篇关于跳过std :: istream中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆