What is the difference between ostream and ostringstream? When would you use one versus the other?
What is the difference between ostream and ostringstream? When would you use one versus
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Put succinctly:
ostringstreamprovides astreambuf,ostreamrequires the user to provide one.
To understand the implications, it’s necessary to understand a little
how streams work, and I’m not sure that there’s a good explanation of this on the Web. The basic abstraction
of
ostreamis formatting textual output. You give it anintor adouble(or a user defined type—more on that later), and itconvert it into a stream of characters, of type
char. What it doeswith that stream depends on the
streambufwhich is attached to it;this is an example of the strategy pattern, where
streambufis anabstract base class of the strategy[1]. The standard provides two
implementations of
streambuf,filebufandstringbuf; in practice,in all but the most trivial applications, you’ll probably have some that
you implement yourself.
When outputting, you always use
ostream; it’s the class over which the<<operators are defined. You’re formatting your data into a streamof characters, and you don’t really care where the stream ends up.
When creating an instance: if you create an
ostream, you must provideit with a
streambufyourself. More often, you’ll create anofstreamor anostringstream. These are both “convenience” classes,which derive from
ostream, and provide astreambuffor it (filebufand
stringbuf, as it happens). Practically speaking, all they do isprovide the necessary
streambuf(which affects the constructor and thedestructor, and not very much else); in the case of
ofstream, thereare also a few extra functions which forward to additional functions in
the
filebufinterface.It’s usual (but by no means required) when you define your own
streambufto provide convenience overloads ofostream(andistream, if relevant), along the same lines asofstreamorostringstream.By the same token, when creating an instance, it’s usual to use one of
the “convenience” derived classes, rather than to use
ostreamdirectlyand provide your own streambuf.
And if all of this seems complicated: the iostream classes use just
about all of the facilities of C++ (virtual functions, templates and
function overloading all play an important role). If you’re just
learning C++, don’t worry too much about it: just use
ofstreamorostringstreamwhen you construct an instance, but pass aroundreferences to
ostream. And as you learn about techniques like virtualfunctions, templates and operator overloading, return to the iostreams
to understand the role they play in making code more flexible.
[1] For various reasons,
std::streambufis not actually abstract. Butthe implementations of the virtual functions in it are useless;
extraction always returns EOF, and insertion always fails.