Memory Based Streams

Stringstreams (defined in the header <sstream>) are in this author's opinion one of the coolest things since sliced time. An example of their use is in the Received Wisdom section for Sect1 21 (Strings), describing how to format strings.

The quick definition is: they are siblings of ifstream and ofstream, and they do for std::string what their siblings do for files. All that work you put into writing << and >> functions for your classes now pays off again! Need to format a string before passing the string to a function? Send your stuff via << to an ostringstream. You've read a string as input and need to parse it? Initialize an istringstream with that string, and then pull pieces out of it with >>. Have a stringstream and need to get a copy of the string inside? Just call the str() member function.

This only works if you've written your <</>> functions correctly, though, and correctly means that they take istreams and ostreams as parameters, not ifstreams and ofstreams. If they take the latter, then your I/O operators will work fine with file streams, but with nothing else -- including stringstreams.

If you are a user of the strstream classes, you need to update your code. You don't have to explicitly append ends to terminate the C-style character array, you don't have to mess with "freezing" functions, and you don't have to manage the memory yourself. The strstreams have been officially deprecated, which means that 1) future revisions of the C++ Standard won't support them, and 2) if you use them, people will laugh at you.