I have the following code:
std::stringstream ss;
ss << 1 << "a b c";
std::string result;
ss >> result;
std::cout << result << std::endl;
I see "1a" instead of "1a b c".
I read somewhere that I should have ss << std::noskip. But it doesn't help.
Any idea?
Thanks in advance.
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.
The classes in <strstream> are deprecated. Consider using the classes in <sstream> instead.
A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file.
The clear() member function is inherited from ios and is used to clear the error state of the stream, e.g. if a file stream has the error state set to eofbit (end-of-file), then calling clear() will set the error state back to goodbit (no error). For clearing the contents of a stringstream , using: m. str("");
std::getline(ss, result);
or, just get string
result = ss.str();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With