A New Library Combining Streams and strings

#include <sstream>

using namespace std;

More later, but here's some code for basic input practices with string streams, basic output practices with string streams, and the bigger lecture example code. (Please note the comment in the istream operator>> that it wasn't a synchronization problem as we'd thought but rather an end-of-file issue.)

Here, also, is a sample code demonstrating the problem with eof() that caused our trouble during lecture. (See also the new notes on eof() elsewhere.)

When to Use Them?

Of course, the obvious use — see the introductory examples — is to translate from numeric types to string version or back. But, other than that, when should you use the *stringstream types?

When you are gathering input from other than a stream source, an istringstream can be useful. Sometimes it can even be useful when your primary data source is a stream if you suspect buffer timing issues are slowing you down. But also beware that duplicating the buffer from the stream into a string can be RAM/heap intensive.

The ostringstream, on the other hand, can be very useful to gather together all the characters that represent a certain class before sending them to your stream for final width formatting. That way your class object acts as a single field for output purposes.