search

Home  >  Q&A  >  body text

c++ - 关于std:cin的一个细节问题

书上看到这个示例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string buf;
    while (cin >> buf && !buf.empty())
        cout << 1;
}

我想问!buf.empty()有必要吗?,据我的理解,cin>>buf不接受空白字符,那就肯定不是空字符串了,所以while(cin>>buf)就行了.

怪我咯怪我咯2803 days ago610

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 12:07:21

    ][1]

    In fact, there is no need to add spaces. By default, spaces and line breaks are not accepted

    reply
    0
  • Cancelreply