Als said:
If no freeze() in stringsream. Then what is
ostringstream::rdbuf()->freeze(0);?
An error, there is no such member in the basic_stringbuf class.
Besides passing in and out stringstream via std::string, my understanding is
it can also be via int or other types. Is this correct?
No. You're confusing two things. A stringstream reads and writes to a
string (as opposed to an fstream that reads and writes to a file).
Could you give an example please to show that strstream needs calling
freeze() and stringstream doesn't please?
Because stringstream HAS no freeze routine to call. When you either
pass in a string to init the buffer or call str() to extract the string from the
buffer, it MAKES a copy. It is therefore not necessary to freeze the
internal buffer because the invoking code has no access to it.
In a strstream you share internal buffer with the calling program. Freezing
it keeps the stringstream from changing it when you don't want that to happen.
In the code:
ostringstream o;
o << "hello world" << ends;
What's "ends" for? My book says that "output null in string" but I am not
sure what it means.
ends writes a null character (i.e., one that is zero valued) to the stream. The
question is whether you really want one. In a (std:
string based stream, nulls
are just another character. For a char* based stream like strstream, you need
the null to signify the end of the data.
I don't know what book you are using but if it uses freeze() with stringstream
it's just wrong and if it uses ends with anything other than a strstream, it's
being stupid.