gpt4 book ai didi

c++ - c++中使用getline()时如何判断是否是EOF?

转载 作者:太空狗 更新时间:2023-10-29 23:48:21 26 4
gpt4 key购买 nike

string s;
getline(cin,s);

while (HOW TO WRITE IT HERE?)
{
inputs.push_back(s);
getline(cin,s);
}

最佳答案

由于我今天懒得给出完整的答案,所以我就把真正有用的 bot in ##c++ on Freenode 说的话贴出来吧:

Using "while (!stream.eof()) {}" is almost certainly wrong. Use the stream's state as the tested value instead: while (std::getline(stream, str)) {}. For further explanation see http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

换句话说,你的代码应该是

string s;

while (getline(cin, s))
{
inputs.push_back(s);
}

关于c++ - c++中使用getline()时如何判断是否是EOF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1264052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com