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?
- 32,434
- 14
- 99
- 159
-
9It's been deprecated for the past 14 years, where've you been? :) – Jonathan Wakely Jun 01 '12 at 11:53
-
1@JonathanWakely: Well, let's say I realised it only some short time ago. Obviously I didn't pay enough attention :) – bitmask Jun 01 '12 at 12:41
5 Answers
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.
-
I am baffled by the downvote! Does anyone see something wrong in there? – Shahbaz Jun 01 '12 at 13:01
-
2You 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. – Jonathan Wakely Jun 01 '12 at 13:08
-
@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. – Shahbaz Jun 01 '12 at 13:12
-
I used to use the dinkumware manuals but last time I checked they'd gone. – Jonathan Wakely Jun 01 '12 at 13:20
-
@JonathanWakely, you see? We need a good reference online, but I can't find any. – Shahbaz Jun 01 '12 at 13:26
-
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. – bitmask Jun 01 '12 at 14:20
-
@bitmask, I changed the link to cppreference.com. I guess good or bad, it can't be as bad as cplusplus.com – Shahbaz Jun 01 '12 at 14:29
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.
- 166,810
- 27
- 341
- 521
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.
- 33,242
- 8
- 53
- 74
-
He "should" in any case? What if he only needs input or only output? In that case I advice against too general solutions. – Sebastian Mach Jun 01 '12 at 13:18
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.
- 18,946
- 11
- 62
- 76
-
1You 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'. – Martin York Jun 01 '12 at 11:58
-
@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`? – betabandido Jun 01 '12 at 12:01
-
http://www.boost.org/doc/libs/1_49_0/libs/format/doc/format.html#printf_directives "%1$.02f" – Martin York Jun 01 '12 at 12:03