Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cout << stringstream

When I put something into a stringstream, let's say a real number, if I then insert that stringstream object into cout...what am I looking at?

Usually I'm getting some strange number. Is this a memory location? Just curious.

It looks like the below comment hit it but here's what I'm trying to do:

string stringIn;  stringstream holdBuff; holdBuff << getline(cin, stringIn); cout << holdBuff;  

Basically I was just trying to see what holdBuff looked like once I inserted stringIn. I am trying to have the user enter a string and then I want to step through it looking for it's contents and possilbly converting...

like image 833
MCP Avatar asked Jan 11 '12 09:01

MCP


People also ask

What is the Stringstream in C++?

The StringStream class in C++ is derived from the iostream class. Similar to other stream-based classes, StringStream in C++ allows performing insertion, extraction, and other operations. It is commonly used in parsing inputs and converting strings to numbers, and vice-versa.

How do you declare a Stringstream in C++?

To use stringstream class in the C++ program, we have to use the header <sstream>. For Example, the code to extract an integer from the string would be: string mystr(“2019”); int myInt; stringstream (mystr)>>myInt; Here we declare a string object with value “2019” and an int object “myInt”.

What does Stringstream mean?

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 stringstream class is extremely useful in parsing input.


1 Answers

What do you think

holdBuff << getline(cin, stringIn); 

is doing. The return type of getline is a reference to the stream being read (cin) in this case. Since there's no << defined which takes an std::istream as second argument, the compiler tries different conversions: in C++11, std::istream has an implicit conversion to bool, and in earlier C++, an implicit conversion to std::ios*, or something similar (but the only valid use of the returned value is to convert it to bool). So you'll either output 1 (C++11), or some random address (in practice, usually the address of the stream, but this is not guaranteed). If you want to get the results of a call to getline into an std::ostringstream, you need two operations (with a check for errors between them):

if ( !getline( std::cin, stringIn ) )     //  Error handling here... holdBuff << stringIn; 

Similarly, to write the contents of a std::ostringstream,

std::cout << holdBuf.str() ; 

is the correct solution. If you insist on using an std::stringstream when an std::ostringstream would be more appropriate, you can also do:

std::cout << holdBuf.rdbuf(); 

The first solution is preferable, however, as it is far more idiomatic.

In any case, once again, there is no << operator that takes any iostream type, so you end up with the results of the implicit conversion to bool or a pointer.

like image 96
James Kanze Avatar answered Oct 17 '22 04:10

James Kanze