A stream is really just a fancy buffer. When you use the >> operator, you're reading characters out of the buffer. When you use the << operator, you're writing characters to the buffer. When the buffer is full, or you use endl, the buffer is written to the underlying device.
stringstreams work the same way, except that the "underlying device" is a string. stringstreams are very handy for performing formatted output to a string. With a simple std::string, you can append data to the string, but a std::string doesn't support the level of custom formatting that a stream does. With a stringstream, one can format output to a string using the same iomanipulators that any stream supports (width, fill, left, right, dec, hex, prec, etc). Once you've formatted the output, you can access the formatted string via the stringstream::str() function.
stringstreams are also handy for dealing with formatted input such as a CSV file. Use delimited form of getline() to read a line of data upto the '\n'. Create a stringstream from the line of data, then use getline again to get data from the line of data field by field, this time delimited by ','.
edit:
just explain exactly what a stream is / does in general? |
This was also explained in your other thread.
http://www.cplusplus.com/forum/general/142052/#msg749868