2D Vector未正确保存并加载了boost :: serialize库

时间:2017-03-30 09:26:00

标签: c++ serialization vector boost

我有一个vector<vector<char>>对象,我想从文件中保存并加载:

class Bar {
  private:
    std::vector<std::vector<char>> _map;

    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        for(int i=0; i<_map.size(); ++i){
            // ar & _map[i];
        }
        ar & _map;
    }
};

虽然for循环有效,但使用ar & _map;的直接方法会使用错误的维度和值来保存和加载地图。

我尝试使用boost:

中的示例代码对其进行测试
// create and modify example object of class Bar here...
Bar s();
std::ofstream ofs("map_save.save");
{
    boost::archive::text_oarchive oa(ofs);
    oa << s;
}

Bar news();
{
    std::ifstream ifs("map_save.save");
    boost::archive::text_iarchive ia(ifs);
    ia >> news;
}

这是不同的2D矢量的样子。 (正确的是最重要的一个:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 C 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
------------
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 C 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 
0 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
0 1 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 0 0 0 0 
1 1 0 0 1 1 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 

1 个答案:

答案 0 :(得分:2)

你正在做其他错误而你没有表现出来:

<强> Live On Coliru

#include <fstream>
#include <iostream>
#include <iomanip>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;

class Bar {
  public:
    void seed(std::vector<std::vector<char>> const& init) { _map = init; }
    bool operator==(Bar const& other) const {
        return _map == other._map;
    }
  private:
    std::vector<std::vector<char>> _map;

    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive& ar, const unsigned int /*version*/) {
        //for(size_t i=0; i<_map.size(); ++i){ ar & _map[i]; }
        ar & _map;
    }
};

int main()
{
    std::vector<std::vector<char>> seed = {
        {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '0', '0', '0', '0', 'C', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '1', '1', '0', '0', '0', '0', '0', '0', '0'},
        {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
    };

    // create and modify example object of class Bar here...
    {
        Bar s;
        s.seed(seed);
        std::ofstream ofs("map_save.save");
        boost::archive::text_oarchive oa(ofs);
        oa << s;
    }

    {
        Bar news;
        std::ifstream ifs("map_save.save");
        boost::archive::text_iarchive ia(ifs);
        ia >> news;


        Bar compareTo;
        compareTo.seed(seed);
        std::cout << "Comparison: " << std::boolalpha << (compareTo == news) << "\n";
    }
}*

打印:

Comparison: true