cin, getline, leading whitespace: ignore vs ws

送分小仙女□ 提交于 2020-02-01 09:15:51

问题


So every once in a while, when I read documentation for getline, I get reminded of the infamous whitespace issue. Yes, I know what it is. This question is about the supposedly standard solution

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Meanwhile, I was always using

std::cin >> std::ws;

The difference between the two being mainly mine one does also ignore whitespace at the beginning of the line. That said, for many uses it is sufficient or equivalent, and arguably simpler to remember.

So why do people always recommend the first one? Not knowing about both? Finding those spaces precious by default?

来源:https://stackoverflow.com/questions/26331385/cin-getline-leading-whitespace-ignore-vs-ws

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!