boost replacement for std::ostringstream
Hello, Does boost provide a replacement for std::ostringstream? If so, can you please tell me the name so I can read the docs on it? The first thing I am going to read is whether or not such a best would provide "reserve". I would like to do this: //----- std::ostringstream Stream; // Stream.reserve(99999); // error: std::ostringstream does not provide reserve Stream.write(pBuffer, 99999); //----- Thank you, Chris
2012/9/6 Chris Stankevitz
Hello,
Does boost provide a replacement for std::ostringstream? If so, can you please tell me the name so I can read the docs on it? The first thing I am going to read is whether or not such a best would provide "reserve". I would like to do this:
//-----
std::ostringstream Stream;
// Stream.reserve(99999); // error: std::ostringstream does not provide reserve
Stream.write(pBuffer, 99999);
You could use filtering_ostream with underlying vector and reserve() on that vector. Take a look at the very bottom of "container_sink" in Tutorials section for usage example. -- Szymon Gatner The Lordz Games Studio www.thelordzgamesstudio.com
On Thu, Sep 6, 2012 at 11:17 PM, Szymon Gatner
You could use filtering_ostream with underlying vector and reserve() on that vector.
Szymon,
Thank you that is exactly what I did:
#include
2012/9/7 Chris Stankevitz
On Thu, Sep 6, 2012 at 11:17 PM, Szymon Gatner
wrote: You could use filtering_ostream with underlying vector and reserve() on that vector.
Szymon,
Thank you that is exactly what I did:
#include
#include #include <string> std::string Result;
Result.reserve(99999);
boost::iostreams::filtering_ostream Stream( boost::iostreams::back_inserter(Result));
Stream.write(pBuffer, 99999); // this part is more complicated in my real scenario
Ah, of course you used string directly. May I ask tho: why are you using a stream to write a block of memory to a string? Why not just directly copying it to a vector (or a string)? If there are no filters attached to a filtering_stream then you just add overhead/complexity without any benefit. Cheers, Simon
On Fri, Sep 7, 2012 at 2:09 PM, Szymon Gatner
May I ask tho: why are you using a stream to write a block of memory to a string? Why not just directly copying it to a vector (or a string)?
These are good questions. The short answer is I am pseudo-profiling... aka profiling by "divine inspiration" rather than actually looking at the results of a profiler... which is often a waste of time but this time seemed to help. My original code wrote many small batches of binary data to std::cout (think hundreds of thousands of calls to std::cout::write). I modified the routine to use boost's filtering_ostream to write many small blocks to a string. Then write that string to stdout with just one call to std::cout::write. This reduced the number of calls to std::cout::write by a factor of 100,000 or so. In my real case, unlike the example I posted here, the data I am writing is not from one contiguous block. Thank you! Chris
participants (2)
-
Chris Stankevitz
-
Szymon Gatner