C++ Boost Serialization 序列化 boost::unordered_map

$ cat serialization_unordered_map.h 
/*******************************************************************************
  # Author : Neo Fung
  # Email : neosfung@gmail.com
  # Last modified: 2011-11-18 18:47
  # Filename: unordered_map_serialization.h
  # Description : 
 ******************************************************************************/
#ifndef BOOST_SERIALIZATION_UNORDEREDMAP_HPP 
#define BOOST_SERIALIZATION_UNORDEREDMAP_HPP 

// MS compatible compilers support #pragma once 
#if defined(_MSC_VER) && (_MSC_VER >= 1020) 
# pragma once 
#endif 

#include <boost/unordered_map.hpp> 

#include <boost/config.hpp> 

#include <boost/serialization/utility.hpp> 
#include <boost/serialization/collections_save_imp.hpp> 
#include <boost/serialization/collections_load_imp.hpp> 
#include <boost/serialization/split_free.hpp> 

namespace boost { 
    namespace serialization { 

        template<class Archive, class Type, class Key, class Hash, class Compare, class Allocator > 
            inline void save( 
                    Archive & ar, 
                    const boost::unordered_map<Key, Type, Hash, Compare, Allocator> &t, 
                    const unsigned int /* file_version */ 
                    ){ 
                boost::serialization::stl::save_collection< 
                    Archive, 
                boost::unordered_map<Key, Type, Hash, Compare, Allocator> 
                    >(ar, t); 
            } 

        template<class Archive, class Type, class Key, class Hash, class Compare, class Allocator > 
            inline void load( 
                    Archive & ar, 
                    boost::unordered_map<Key, Type, Hash, Compare, Allocator> &t, 
                    const unsigned int /* file_version */ 
                    ){ 
                boost::serialization::stl::load_collection< 
                    Archive, 
                boost::unordered_map<Key, Type, Hash, Compare, Allocator>, 
                boost::serialization::stl::archive_input_map< 
                    Archive, boost::unordered_map<Key, Type, Hash, Compare, Allocator> >, 
                boost::serialization::stl::no_reserve_imp<boost::unordered_map< 
                    Key, Type, Hash, Compare, Allocator 
                    > 
                    > 
                    >(ar, t); 
            } 

        // split non-intrusive serialization function member into separate 
        // non intrusive save/load member functions 
        template<class Archive, class Type, class Key, class Hash, class Compare, class Allocator > 
            inline void serialize( 
                    Archive & ar, 
                    boost::unordered_map<Key, Type, Hash, Compare, Allocator> &t, 
                    const unsigned int file_version 
                    ){ 
                boost::serialization::split_free(ar, t, file_version); 
            } 

        // multimap 
        template<class Archive, class Type, class Key, class Hash, class Compare, class Allocator > 
            inline void save( 
                    Archive & ar, 
                    const boost::unordered_multimap<Key, Type, Hash, Compare, Allocator> &t, 
                    const unsigned int /* file_version */ 
                    ){ 
                boost::serialization::stl::save_collection< 
                    Archive, 
                boost::unordered_multimap<Key, Type, Hash, Compare, Allocator> 
                    >(ar, t); 
            } 
#if 0
        template<class Archive, class Type, class Key, class Hash, class Compare, class Allocator > 
            inline void load( 
                    Archive & ar, 
                    boost::unordered_multimap<Key, Type, Hash, Compare, Allocator> &t, 
                    const unsigned int /* file_version */ 
                    ){ 
                boost::serialization::stl::load_collection< 
                    Archive, 
                boost::unordered_multimap<Key, Type, Hash, Compare, Allocator>, 
                boost::serialization::stl::archive_input_multimap< 
                    Archive, boost::unordered_multimap<Key, Type, Hash, Compare, Allocator> 
                    >, 
                boost::serialization::stl::no_reserve_imp< 
                    boost::unordered_multimap<Key, Type, Hash, Compare, Allocator> 
                    > 
                    >(ar, t); 
            } 

        // split non-intrusive serialization function member into separate 
        // non intrusive save/load member functions 
        template<class Archive, class Type, class Key, class Hash, class Compare, class Allocator > 
            inline void serialize( 
                    Archive & ar, 
                    boost::unordered_multimap<Key, Type, Hash, Compare, Allocator> &t, 
                    const unsigned int file_version 
                    ){ 
                boost::serialization::split_free(ar, t, file_version); 
            } 
#endif

    } // serialization 
} // namespace boost 

#endif // BOOST_SERIALIZATION_UNORDEREDMAP_HPP

 

 

$ cat t4.cpp 
#include "serialization_unordered_map.h"
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <string>
#include <fstream>
#include <vector>

typedef boost::unordered_map<std::string, std::vector<float>> map_type;

int main(int argc, char ** argv)
{
    map_type my_map;
    std::vector<float> vec;

    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    my_map["123"] = vec;

    {
        std::ofstream ofs("unordered_map");
        boost::archive::text_oarchive oa(ofs);
        oa << my_map;
    }
    {
        map_type my_map2;
        std::ifstream ifs("unordered_map");
        boost::archive::text_iarchive ia(ifs);
        ia >> my_map2;
        for(auto it = my_map2.begin(); it != my_map2.end(); it++) {
            for(int loop = 0; loop < it->second.size(); loop++) {
                printf("key: %s; id: %d; value: %f\n", it->first.c_str(), loop, it->second[loop]);
            }
        }
    }
    {
        std::ofstream ofs("unordered_map_binary");
        boost::archive::binary_oarchive oa(ofs);
        oa << my_map;
    }
    {
        map_type my_map2;
        std::ifstream ifs("unordered_map_binary");
        boost::archive::binary_iarchive ia(ifs);
        ia >> my_map2;
        for(auto it = my_map2.begin(); it != my_map2.end(); it++) {
            for(int loop = 0; loop < it->second.size(); loop++) {
                printf("key: %s; id: %d; value: %f\n", it->first.c_str(), loop, it->second[loop]);
            }
        }
    }

    return 0;
}

 

编译:

g++ t4.cpp -lboost_serialization  -std=c++11

 

原出处: https://blog.csdn.net/neofung/article/details/6989421?locationNum=12&fps=1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值