Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringstream doesn't accept white space?

Tags:

c++

g++

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.

like image 232
Hei Avatar asked Jun 05 '13 08:06

Hei


People also ask

Does Stringstream ignore whitespace C++?

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.

Is Stringstream deprecated?

The classes in <strstream> are deprecated. Consider using the classes in <sstream> instead.

What should I use for Stringstream?

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.

How do you clear a Stringstream variable?

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("");


1 Answers

std::getline(ss, result);

or, just get string

result = ss.str();
like image 136
ForEveR Avatar answered Oct 17 '22 04:10

ForEveR