如何对地图使用boost :: serialize :: make_binary_object

时间:2017-03-28 15:10:30

标签: c++ boost boost-serialization

我的目的是从map<int,std::string>获取输入并创建以下代码无法执行的二进制存档,这是创建它的正确方法吗? 以及如何知道数据是否正确存档(使用boost进行预期) 我只需创建一个boost::archive::binary_iarchive oa(oss,1);并将其复制到oss?

#include <fstream>
#include <boost/serialization/map.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <string>
#include <iostream>
#include <map>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>

using namespace std;

class MyConnections
{
  public:
    MyConnections()
    {

       e_group.insert( std::make_pair(1,"ETOTO") ) ;
       e_group.insert( std::make_pair(2,"ETOTO") ) ;

   }

 template<class archive>
void serialize(archive& ar, const unsigned int version)
{
    using boost::serialization::make_binary_object;
    ar &  boost::serialization::make_binary_object(e_group);
}


  public:
   typedef   map<int,std::string> groups;
   groups  e_group;
};

int main(int argc, char* argv[])
{
    const MyConnections conn;
    stringstream oss(ios_base::out|ios_base::binary);
    boost::archive::binary_oarchive oa(oss,1);
    oa << boost::serialization::make_binary_object(conn)
    std::cout<<oss.str;
}

错误

main.cpp: In member function 'void MyConnections::serialize(archive&, unsigned int)':
main.cpp:29:63: error: cannot convert 'MyConnections::groups {aka std::map<int, std::__cxx11::basic_string<char> >}' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
         ar &  boost::serialization::make_binary_object(e_group);
                                                               ^
main.cpp: In function 'int main(int, char**)':
main.cpp:42:55: error: cannot convert 'const MyConnections' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
    oa << boost::serialization::make_binary_object(conn)
                                                   ^

1 个答案:

答案 0 :(得分:1)

标准输出不支持随机二进制输出。您可能使用hex64编码的十六进制。

这是写出十六进制字节的简单方法:

#include <sstream>
#include <iostream>
#include <iomanip>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/map.hpp>

using namespace std;

class MyConnections
{
    public:
        MyConnections()
        {

            e_group.insert( std::make_pair(1,"ETOTO") ) ;
            e_group.insert( std::make_pair(2,"ETOTO") ) ;

        }

        template<class archive>
            void serialize(archive& ar, const unsigned int /*version*/) {
                using boost::serialization::make_nvp;
                ar & make_nvp("Connections", e_group);
            }

    public:
        typedef   map<int,std::string> groups;
        groups  e_group;
};

std::string foo() {
    const MyConnections conn;

    stringstream oss(ios_base::out|ios_base::binary);
    boost::archive::binary_oarchive oa(oss);
    oa << boost::serialization::make_nvp("Connections", conn);
    std::string data = oss.str();
    return oss.str();
}

int main()
{
    std::string const data = foo();

    for (uint8_t ch : data) {
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(ch) << " ";
    }
}

打印:

    00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 45 54 4f 54 4f 02 00 00 00 05 00 00 00 00 00 00 00 45 54 4f 54 4f 

查看 Live On Coliru

  

注意:我认为归档构造函数的,1参数很奇怪。如果您要传递标记,请使用正确的枚举值。