cin, getline, leading whitespace: ignore vs ws

2020-07-31 03:28发布

问题:

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?

标签: c++ iostream