Output stream class to operate on strings.
Questions tagged [ostringstream]
150 questions
208
votes
8 answers
What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?
When would I use std::istringstream, std::ostringstream and std::stringstream and why shouldn't I just use std::stringstream in every scenario (are there any runtime performance issues?).
Lastly, is there anything bad about this (instead of using a…

Oliver Baur
- 2,295
- 2
- 16
- 7
122
votes
4 answers
How to reuse an ostringstream?
I'd like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn't have to do as many allocations. How do I reset the object to its initial state?

twk
- 16,760
- 23
- 73
- 97
34
votes
6 answers
Move the string out of a std::ostringstream
If I construct a string made of a list of space separated floating point values using std::ostringstream:
std::ostringstream ss;
unsigned int s = floatData.size();
for(unsigned int i=0;i

galinette
- 8,896
- 2
- 36
- 87
26
votes
2 answers
How to hook up Boost serialization & iostreams to serialize & gzip an object to string?
I've been using the Boost serialization library, which is actually pretty nice, and lets me make simple wrappers to save my serializable objects to strings, like so:
namespace bar = boost::archive;
namespace bio = boost::iostreams;
template

cce
- 4,874
- 2
- 28
- 25
26
votes
1 answer
How do I use the ostringstream properly in c++?
I am attempting to return some information when my toString() method is called, which include an integer and some floats. I learned about ostringstream works great but when the class that contains this method is called over and over again, the…

Arminium
- 391
- 2
- 6
- 12
20
votes
7 answers
Is there anyway to write the following as a C++ macro?
my_macro << 1 << "hello world" << blah->getValue() << std::endl;
should expand into:
std::ostringstream oss;
oss << 1 << "hello world" << blah->getValue() << std::endl;
ThreadSafeLogging(oss.str());

anon
- 41,035
- 53
- 197
- 293
19
votes
3 answers
What is the purpose of ostringstream's string constructor?
On MSVC 2005, I have the following code.
std::ostringstream stream("initial string ");
stream << 5;
std::cout << stream.str();
What I expect is:
initial string 5
What I get is:
5nitial string
Initializing the stream with a string, I would expect…

Jesse Stimpson
- 972
- 9
- 19
19
votes
4 answers
C++: What is the difference between ostream and ostringstream?
What is the difference between ostream and ostringstream? When would you use one versus the other?

kmccoy
- 2,761
- 3
- 25
- 29
15
votes
7 answers
C++ format macro / inline ostringstream
I'm trying to write a macro that would allow me to do something like: FORMAT(a << "b" << c << d), and the result would be a string -- the same as creating an ostringstream, inserting a...d, and returning .str(). Something like:
string f(){
…

cadabra
- 384
- 3
- 8
14
votes
2 answers
What use is there for 'ends' these days?
I came across a subtle bug a couple of days ago where the code looked something like this:
ostringstream ss;
int anInt( 7 );
ss << anInt << "HABITS";
ss << ends;
string theWholeLot = ss.str();
The problem was that the ends was sticking a '\0' into…

Component 10
- 10,247
- 7
- 47
- 64
10
votes
3 answers
Avoid Copying string from ostringstream
I have a named std::string that I want to fill with data via an std::ostream interface and avoid a string copy.
One way to do it which does involve a copy is to do this:
bool f(std::string& out)
{
std::ostringstream ostr;
fillWithData(ostr);
…

Adi Shavit
- 16,743
- 5
- 67
- 137
9
votes
1 answer
Why does std::boolalpha ignore field width when using clang?
The following code gives different results with the g++ 7 compiler and Apple clang++. Did I run into a bug in clang in the alignment of bool output when std::boolalpha is used, or did I make a mistake?
#include
#include
#include…

Chiel
- 6,006
- 2
- 32
- 57
8
votes
4 answers
Rounding off floats with ostringstream
I have an issue regarding conversion from float to c++ string using ostringstream. Here is my line:
void doSomething(float t)
{
ostringstream stream;
stream << t;
cout << stream.str();
}
when t has value -0.89999 it is round off to…

boom
- 5,856
- 24
- 61
- 96
8
votes
4 answers
Is there a way to reduce ostringstream malloc/free's?
I am writing an embedded app. In some places, I use std::ostringstream a lot, since it is very convenient for my purposes. However, I just discovered that the performance hit is extreme since adding data to the stream results in a lot of calls to…

Johan Kotlinski
- 25,185
- 9
- 78
- 101
7
votes
4 answers
Is there a more efficient way to set a std::vector from a stream?
Presently, I set the value of a std::vector from an std::ostringstream as follows:
void
foo(std::vector &data, std::stringstream &stream) {
data = std::vector(stream.str().begin(), stream.str().end());
}
I'm wondering if there…

WilliamKF
- 41,123
- 68
- 193
- 295