getline 与 istream_iterator
如果您要从文件中逐行输入(将行读入字符串,以进行标记化),是否应该有理由选择 getline 或 istream_iterator 。
Should there be a reason to preffer either getline or istream_iterator if you are doing line by line input from a file(reading the line into a string, for tokenization).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有时(根据情况)编写一个行类,这样我就可以使用 istream_iterator :
I sometimes (depending on the situation) write a line class so I can use
istream_iterator
:getline
将为您提供整行,而istream_iterator
将为您提供单个单词(以空格分隔)。取决于您想要完成的任务,如果您问哪个更好(标记化只是一位,例如,如果您期望一个结构良好的程序并且希望解释它,那么整行阅读可能会更好。 .)
getline
will get you the entire line, whereasistream_iterator<std::string>
will give you individual words (separated by whitespace).Depends on what you are trying to accomplish, if you are asking which is better (tokenization is just one bit, e.g. if you are expecting a well formed program and you expect to interpret it, it may be better to read in entire lines...)
@Martin York 的答案虽然有效,但在与 STL 算法一起使用时在许多方面都失败了。一个更简单的解决方案是使用继承。
@Martin York's answer-- while works-- fails in many areas when used with STL's algorithm. A simpler solution is to use inheritance.