std :: getline()返回 [英] std::getline() returns

查看:145
本文介绍了std :: getline()返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,使用getline()读取文件中的每一行。

I have a loop that reads each line in a file using getline().

istream is;
string line;
while (!getline(is, line).eof())
{
}

我注意到这样调用getline()似乎也工作:

I noticed that calling getline() like this also seems to work:

while (getline(is, line))

这是怎么回事? getline()返回流引用。是不是被转换为指针?

What's going on here? getline() returns a stream reference. Is it being converted to a pointer somehow? Is this actually a good practice or should I stick to the first form?

推荐答案

getline()返回的istream有它的operator void *()方法隐式调用,它返回流是否遇到错误。因此,它比调用eof()进行更多的检查。

The istream returned by getline() is having its operator void*() method implicitly called, which returns whether the stream has run into an error. As such it's making more checks than a call to eof().

这篇关于std :: getline()返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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