Reputation: 33
I was reading about stringstream and found this example.
stringstream ss;
int foo;
int bar = 0;
ss << 100 << ' ' << 200;
ss >> foo;
ss >> bar;
cout << "foo: " << foo << '\n'; // 100
cout << "bar: " << bar << '\n'; // 200
Why the value of variable 'bar' is 200? Shouldn't a 'bar' read a space from stream and get an ASCII code for space (32), or some other values? Does stringstream ignoring whitespaces? Then why if we add a line here:
ss >> foo;
ss.ignore(1);
ss >> bar;
the bar's value still 200 isntead of 0?
Upvotes: 2
Views: 5681
Reputation: 96800
Does stringstream ignoring whitespaces?
Technically the operator>>()
function ignores leading whitespace. Doing ss.ignore(1)
is a way to do it manually, but it's unnecessary.
On the other hand, the function std::getline()
does not skip whitespace. So if there is leading whitespace you will have to remove it manually.
Upvotes: 1