What should I use instead of std::ostrstream?
Asked Answered
S

5

11

I like to use std::ostrstream to format text but not print it to stdout but instead write it into an std::string (by accessing the std::ostrstream::str() member). Apparently this is deprecated now. So, how am I supposed to write formatted objects to a string, with the same convenience as when writing to a stream?

Sitka answered 1/6, 2012 at 11:46 Comment(2)
It's been deprecated for the past 14 years, where've you been? :)Dog
@JonathanWakely: Well, let's say I realised it only some short time ago. Obviously I didn't pay enough attention :)Sitka
M
19

You could use std::ostringstream. Similarly, instead of std::istrstream you should use std::istringstream. You need to include the <sstream> header for these classes.

You could also see this question which explains why strstream was deprecated.

Mika answered 1/6, 2012 at 11:48 Comment(7)
I am baffled by the downvote! Does anyone see something wrong in there?Mika
You got an upvote from me, but I would guess the downvote was because you linked to cplusplus.com, which is terrible and full of bad style and some outright errors.Dog
@JonathanWakely, I am aware that cplusplus.com sucks. I try to avoid it myself always. Do you know a reliable website I can include? The problem is that searched on google always brings that website up first. When you go further down, you see MSDN (which I wouldn't even consider for an instance) and then you are out of reference sites and into Q&A sites such as stackoverflow.Mika
I used to use the dinkumware manuals but last time I checked they'd gone.Dog
@JonathanWakely, you see? We need a good reference online, but I can't find any.Mika
I'm under the impression that cppreference.com (which I linked to in the OP) is better than cplusplus.com but I've nothing to back that up, unfortunately.Sitka
@bitmask, I changed the link to cppreference.com. I guess good or bad, it can't be as bad as cplusplus.comMika
D
3

As others have already said, std::ostringstream is the replacement.

It's more convenient (and safer) than std::ostrstream because it manages all memory automatically so you don't need to call freeze(false) to ensure the memory gets freed when you're finished with it.

Dog answered 1/6, 2012 at 11:56 Comment(0)
A
1

You should use std::stringstream. Also, see boost::lexical_cast.

std::stringstream supports both << and >>. std::ostringstream only supports <<, and std::istringstream only supports >>. Often I find it convenient to be able to use both operators.

Addington answered 1/6, 2012 at 11:48 Comment(1)
He "should" in any case? What if he only needs input or only output? In that case I advice against too general solutions.Befitting
T
1

With C++20 you can use std::format

Tessin answered 23/5, 2023 at 4:46 Comment(0)
C
0

You can also use boost::format. Then you can do things like:

int a = 1;
std::string b("foo");
std::string s = boost::str(
    boost::format("some text, some vars=%1%, %2%, %1%") % a % b % a);

Then s would contain "some text, some vars=1, foo, 1".

This is, in my opinion, more convenient in some cases than using operator <<.

As a reference, I also include the format specification.

Chromatism answered 1/6, 2012 at 11:52 Comment(3)
You want to stop using '%d' and other type specific stuff as this will lead to the same problems as sprintf(). Prefer the index specific identifiers '%1'.Beaubeauchamp
@LokiAstari thanks for that! I didn't know about it. I'll update my answer accordingly. But what if you just want to show floating point numbers with two decimals? Is there any other way than using %.02f?Chromatism
boost.org/doc/libs/1_49_0/libs/format/doc/… "%1$.02f"Beaubeauchamp

© 2022 - 2024 — McMap. All rights reserved.