简体   繁体   English

将STL映射存储到Boost ConstBufferSequence中

[英]Store a STL map into a Boost ConstBufferSequence

I'm trying to store a std::map<enum, int> in a boost::beast::multi_buffer . 我正在尝试将std::map<enum, int>boost::beast::multi_buffer So far I've been using boost::asio::buffer_copy and boost::asio::buffer to store vectors and PODs. 到目前为止,我一直在使用boost::asio::buffer_copyboost::asio::buffer来存储矢量和POD。 However, I couldn't find a way to store a STL map. 但是,我找不到存储STL映射的方法。

I've tried this: 我已经试过了:

auto t_map = std::map<CODES, int>(); // CODES is an enum type

auto t_map_size = t_map.size() * sizeof(std::pair<CODES, int>);
auto tmp_buffer = boost::asio::buffer(t_map, t_map_size); // this is not supported

auto size = boost::asio::buffer_copy(t_map_size , tmp_buffer);

boost::beast::multi_buffer buffer;
buffer.commit(size);
  1. Is there any way to store a std::map<enum, int> in a ConstBufferSequence ? 有什么方法可以在ConstBufferSequence存储std::map<enum, int>吗? (because the boost::asio::buffer_copy needs one) (因为boost::asio::buffer_copy需要一个)
  2. If not, is there any workaround to store a std::map<enum, int> in a boost::beast::multi_buffer ? 如果没有,是否有任何变通办法将std::map<enum, int>boost::beast::multi_buffer

Thanks! 谢谢!

You can convert your map into vector of POD, as POD choose type which can store enum + int, for example int64_t : 您可以将地图转换为POD的向量,因为POD选择可以存储enum + int的类型,例如int64_t

So create vector, scan your map creating items of vector using some bitwise operations and data is ready: 因此,创建矢量,使用一些按位操作扫描您的地图以创建矢量项目,并且数据已准备就绪:

auto t_map = std::map<CODES, int>(); // CODES is an enum type

std::vector<int64_t> vec;
for (auto&& elemMap : t_map)
{
    int64_t val = elemMap.first;   // store key
    val <<= 32; // shift key
    val |= elemMap.second; // store value

    vec.push_back (val);
}

auto tmp_buffer = boost::asio::buffer(vec); // this is supported

Unpack vector into map should be easy. 将向量解压缩到地图中应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM