When would I use std::istringstream
, std::ostringstream
and std::stringstream
and why shouldn\'t I just use std::stringstream
A stringstream
is somewhat larger, and might have slightly lower performance -- multiple inheritance can require an adjustment to the vtable pointer. The main difference is (at least in theory) better expressing your intent, and preventing you from accidentally using >>
where you intended <<
(or vice versa). OTOH, the difference is sufficiently small that especially for quick bits of demonstration code and such, I'm lazy and just use stringstream
. I can't quite remember the last time I accidentally used <<
when I intended >>
, so to me that bit of safety seems mostly theoretical (especially since if you do make such a mistake, it'll almost always be really obvious almost immediately).
Nothing at all wrong with just using a string, as long as it accomplishes what you want. If you're just putting strings together, it's easy and works fine. If you want to format other kinds of data though, a stringstream
will support that, and a string mostly won't.