c++ - efficient copy of data from boost::asio::streambuf to std::string -


i need copy content of (boost::asio::)streambuf std::string.

the following code works, think there's unnecessary copy between _msg , temporary std::string:

msg (boost::asio::streambuf & sb, size_t bytes_transferred) :     _nbytesinmsg    (bytes_transferred) {     boost::asio::streambuf::const_buffers_type buf = sb.data();      _msg = std::string(         boost::asio::buffers_begin(buf),         boost::asio::buffers_begin(buf) + _nbytesinmsg); } 

i tried replacing following:

     _msg.reserve(_nbytesinmsg);      std::copy(         boost::asio::buffers_begin(buf),         boost::asio::buffers_begin(buf) + _nbytesinmsg,         _msg.begin()     ); 

while compiles, doesn't copy _msg string.

will compiler (gcc4.4.7) optimize case - e.g. copy streambuf straight _msg without using temporary?

is there perhaps iterator can use boost::asio::streambuf::const_buffers_type in order make std::copy work instead?

reserve doesn't mean think means. can work out sure means reading documentation. not same resize; reserving space not affect container's size.

you need insert elements string. this:

#include <algorithm>    // copy #include <iterator>     // back_inserter  _msg.reserve(_nbytesinmsg);  // insert _nbytesinmsg elements  std::copy(     boost::asio::buffers_begin(buf),     boost::asio::buffers_begin(buf) + _nbytesinmsg,     std::back_inserter(_msg) ); 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -