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

那年仲夏 提交于 2019-12-11 03:17:53

问题


I need to copy the content of a (boost::asio::)streambuf to an std::string.

The following code works, but I think that there's an unnecessary copy between _msg and the 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 with the following:

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

While this compiles, it doesn't copy anything to the _msg string.

Will the compiler (gcc4.4.7) optimize this case - e.g. copy the streambuf straight to _msg without using a temporary?

Is there perhaps an iterator I can use with boost::asio::streambuf::const_buffers_type in order to make the std::copy work instead?


回答1:


reserve doesn't mean what you think it means. You can work out for sure what it means by reading its documentation. It is not the same as resize; reserving space does not affect the container's size.

You need to actually insert elements into the string. Do this:

#include <algorithm>    // for copy
#include <iterator>     // for back_inserter

_msg.reserve(_nBytesInMsg);  // about to insert _nBytesInMsg elements

std::copy(
    boost::asio::buffers_begin(buf),
    boost::asio::buffers_begin(buf) + _nBytesInMsg,
    std::back_inserter(_msg)
);


来源:https://stackoverflow.com/questions/27015803/efficient-copy-of-data-from-boostasiostreambuf-to-stdstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!