serialize std :: unordered_map&lt; int,std :: unordered_set <int>&gt;加强

时间:2015-07-30 18:30:21

标签: c++ c++11 serialization boost

我尝试序列化/反序列化 std::unordered_map < int, std::unordered_set<int> > 当我看看boost / serialization / map.hpp时,它似乎很简单(甚至很难,我也不太明白) 以下代码似乎编译为序列化但无法反序列化。

有人知道如何正确地做到这一点,还是可以指出这些STL序列化技术的一些文档?我遗憾地找不到东西......

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <boost/serialization/map.hpp>
#include <boost/serialization/collections_save_imp.hpp>
#include <boost/serialization/collections_load_imp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/split_free.hpp>

namespace boost { namespace serialization {

    template<class Archive, typename... TArgs >
        inline void save(Archive & ar, std::unordered_map<TArgs...> const&t, unsigned) {
            boost::serialization::stl::save_collection<Archive, std::unordered_map<TArgs...> >(ar, t);
        }

    template<class Archive, typename... TArgs >
        inline void load(Archive & ar, std::unordered_map<TArgs...> &t, unsigned) {
            boost::serialization::stl::load_collection<Archive,
                std::unordered_map<TArgs...>,
                boost::serialization::stl::archive_input_map<
                    Archive, std::unordered_map<TArgs...> >,
                boost::serialization::stl::no_reserve_imp<std::unordered_map<TArgs...> >
                    >(ar, t);
        }

    // split non-intrusive serialization function member into separate
    // non intrusive save/load member functions
    template <class Archive, typename... TArgs>
        inline void serialize(Archive & ar, std::unordered_map<TArgs...> &t, unsigned file_version) {
            boost::serialization::split_free(ar, t, file_version);
        }
} }


namespace boost { namespace serialization {

    template<class Archive, typename... TArgs >
        inline void save(Archive & ar, std::unordered_set<TArgs...> const&t, unsigned) {
            boost::serialization::stl::save_collection<Archive, std::unordered_set<TArgs...> >(ar, t);
        }

    template<class Archive, typename... TArgs >
        inline void load(Archive & ar, std::unordered_set<TArgs...> &t, unsigned) {
            boost::serialization::stl::load_collection<Archive,
                std::unordered_set<TArgs...>,
                boost::serialization::stl::archive_input_map<
                    Archive, std::unordered_set<TArgs...> >,
                boost::serialization::stl::no_reserve_imp<std::unordered_set<TArgs...> >
                    >(ar, t);
        }

    // split non-intrusive serialization function member into separate
    // non intrusive save/load member functions
    template <class Archive, typename... TArgs>
        inline void serialize(Archive & ar, std::unordered_set<TArgs...> &t, unsigned file_version) {
            boost::serialization::split_free(ar, t, file_version);
        }
} }


int main()
{

    std::stringstream ss;
    boost::archive::binary_oarchive oa2(ss, boost::archive::no_codecvt | boost::archive::no_header);

    std::unordered_map<int, std::unordered_set<int> > s, out;
    s.emplace( 0, std::unordered_set<int>{9,19} );

    oa2 << s;

    // Try to load this!!
    //boost::archive::binary_iarchive ia2(ss, boost::archive::no_codecvt | boost::archive::no_header);
    //ia2 >> out;
}

http://coliru.stacked-crooked.com/a/663c0ce7bf632f85

1 个答案:

答案 0 :(得分:7)

Boost已经包含自1.5.6版以来序列化std::unordered_mapstd::unordered_set的方法(实际上它们是added in February 2014

因此无需自己编写:

#include <map>
#include <sstream>

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/unordered_set.hpp>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

int main()
{
  std::stringstream ss;

  std::unordered_map<int, std::unordered_set<int> > s, out;
  s.emplace( 0, std::unordered_set<int>{9,19} );
  boost::archive::text_oarchive oarch(ss);
  oarch << s;
  boost::archive::text_iarchive iarch(ss);
  iarch >> out;
  std::cout << (s == out) << std::endl;
}

live on coliru