What’s the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

Personally, I find it very rare that I want to perform streaming into and out of the same string stream. Usually I want to either initialize a stream from a string and then parse it; or stream things to a string stream and then extract the result and store it. If you’re streaming to and … Read more

Is there anyway to write the following as a C++ macro?

#define my_macro my_stream() class my_stream: public std::ostringstream { public: my_stream() {} ~my_stream() { ThreadSafeLogging(this->str()); } }; int main() { my_macro << 1 << “hello world” << std::endl; } A temporary of type my_stream is created, which is a subclass of ostringstream. All operations to that temporary work as they would on an ostringstream. When the … Read more

How to reuse an ostringstream?

I’ve used a sequence of clear and str in the past: // clear, because eof or other bits may be still set. s.clear(); s.str(“”); Which has done the thing for both input and output stringstreams. Alternatively, you can manually clear, then seek the appropriate sequence to the begin: s.clear(); s.seekp(0); // for outputs: seek put … Read more