Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate std::stringbuf based on std::vector<char>
    text
    copied!<p>I am doing in-memory image conversions between two frameworks (OpenSceneGraph and wxWidgets). Not wanting to care about the underlying classes (<code>osg::Image</code> and <code>wxImage</code>), I use the stream oriented I/O features both APIs provide like so:</p> <p>1) Create an <code>std::stringstream</code></p> <p>2) Write to the stream using OSG's writers</p> <p>3) Read from the stream using wxWigdets readers</p> <p>This works fairly well. Until now I've been using direct access to the stream buffer, but my attention has been recently caught by the "non-contiguous underlying buffer" problem of the <code>std::stringstream</code>. I had been using a kludge to get a <code>const char*</code> ptr to the buffer - but it worked (tested on Windows, Linux and OSX, using MSVC 9 and GCC 4.x), so I never fixed it.</p> <p>Now I understand that this code is a time bomb and I want to get rid of it. This problem has been brought up several times on SO (<a href="https://stackoverflow.com/questions/1877500/c-stl-stringstream-direct-buffer-access">here for instance</a>), but I could not find an answer that could really help me do the simplest thing that could possibly work.</p> <p>I think the most reasonable thing to do is to create my own streambuf using a vector behind the scenes - this would guarantee that the buffer is contiguous. I am aware that this would not a generic solution, but given my constraints:</p> <p>1) the required size is <em>not</em> infinite and actually quite predictable</p> <p>2) my stream really needs to be an <code>std::iostream</code> (I can't use a raw char array) because of the APIs</p> <p>anybody knows how I can either a custom stringbuf using a vector of chars ? Please do not answer "use <code>std::stringstream::str()</code>", since I <em>know</em> we can, but I'm precisely looking for something else (even though you'd say that copying 2-3 MB is so fast that I wouldn't even notice the difference, let's consider I am still interested in custom stringbufs just for the beauty of the exercise).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload