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<s;i++)
{
ss << floatData[i] << " ";
}
Then I get the result in a std::string
:
std::string textValues(ss.str());
However, this will cause an unnecessary deep copy of the string contents, as ss
will not be used anymore.
Is there any way to construct the string without copying the entire content?
I implemented "outstringstream" class, which I believe does exactly what you need (see take_str() method). I partially used code from: What is wrong with my implementation of overflow()?
+1 for the Boost Karma by @Cubbi and the suggestion to "create your own
streambuf
-dervied type that does not make a copy, and give that to the constructor of abasic_istream<>
.".A more generic answer, though, is missing, and sits between these two. It uses Boost Iostreams:
Here's a demo program:
Live On Coliru
Even better, you can use it with the
array_sink
device and have a fixed-size buffer. That way you can avoid any buffer allocation whatsoever with your Iostreams code!Live On Coliru
Both programs print:
std::ostringstream
offers no public interface to access its in-memory buffer unless it non-portably supportspubsetbuf
(but even then your buffer is fixed-size, see cppreference example)If you want to torture some string streams, you could access the buffer using the protected interface:
The standard C++ way of directly accessing an auto-resizing output stream buffer is offered by
std::ostrstream
, deprecated in C++98, but still standard C++14 and counting.However, I think the cleanest (and the fastest) solution is boost.karma
Update: In the face of people's continued dislike of this answer, I thought I'd make an edit and explain.
No, there is no way to avoid a string copy (stringbuf has the same interface)
It will never matter.It's actually more efficient that way. (I will try to explain this)Imagine writing a version of
stringbuf
that keeps a perfect, moveablestd::string
available at all times. (I have actually tried this).Adding characters is easy - we simply use
push_back
on the underlying string.OK, but what about removing characters (reading from the buffer)? We'll have to move some pointer to account for the characters we've removed, all well and good.
However, we have a problem - the contract we're keeping that says we'll always have a
std::string
available.So whenever we remove characters from the stream, we'll need to
erase
them from the underlying string. That means shuffling all the remaining characters down (memmove
/memcpy
). Because this contract must be kept every time the flow of control leaves our private implementation, this in practice means having to erase characters from the string every time we callgetc
orgets
on the string buffer. This translates to a call to erase on every<<
operation on the stream.Then of course there's the problem of implementing the pushback buffer. If you pushback characters into the underlying string, you've got to
insert
them at position 0 - shuffling the entire buffer up.The long and short of it is that you can write an ostream-only stream buffer purely for building a
std::string
. You'll still need to deal with all the reallocations as the underlying buffer grows, so in the end you get to save exactly one string copy. So perhaps we go from 4 string copies (and calls to malloc/free) to 3, or 3 to 2.You'll also need to deal with the problem that the streambuf interface is not split into
istreambuf
andostreambuf
. This means you still have to offer the input interface and either throw exceptions or assert if someone uses it. This amounts to lying to users - we've failed to implement an expected interface.For this tiny improvement in performance, we must pay the cost of:
developing a (quite complex, when you factor in locale management) software component.
suffering the loss of flexibility of having a streambuf which only supports output operations.
Laying landmines for future developers to step on.