I use them mainly as memory buffers when creating messages:
if(someVector.size() > MAX_SIZE) { ostringstream buffer; buffer << "Vector should not have " << someVector.size() << " eleements"; throw std::runtime_error(buffer.str()); }
or to build complex strings:
std::string MyObject::GenerateDumpPath() { using namespace std; std::ostringstream dumpPath; // add the file name dumpPath << "\\myobject." << setw(3) << setfill('0') << uniqueFileId << "." << boost::lexical_cast<std::string>(state) << "_" << ymd.year << "." << setw(2) << setfill('0') << ymd.month.as_number() << "." << ymd.day.as_number() << "_" << time.hours() << "." << time.minutes() << "." << time.seconds() << ".xml"; return dumpPath.str(); }
This is useful because it brings the full extensibility of std::stream to the use of character buffers (support for extensibility and ostreams locales, buffer memory management, etc.).
Another example I saw is an error message in the gsoap library using dependency injection: soap_stream_fault accepts the ostream command & parameter to report error messages.
If you want, you can pass it std :: cerr, std :: cout or std :: ostringstream (I use it with the implementation of std :: ostringstream).
utnapistim
source share