Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 2D矢量未正确保存并与boost::serialize库一起加载_C++_Serialization_Vector_Boost - Fatal编程技术网

C++ 2D矢量未正确保存并与boost::serialize库一起加载

C++ 2D矢量未正确保存并与boost::serialize库一起加载,c++,serialization,vector,boost,C++,Serialization,Vector,Boost,我有一个vector对象,我想从文件中保存和加载它: 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)

我有一个
vector
对象,我想从文件中保存和加载它:

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;
    }
};
这就是不同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 

你正在做一些你没有表现出来的错误:

#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";
    }
}*

为什么省略了无法编译的相关代码和post代码?我认为我的错误在于我使用boost序列化的方式。对于这个糟糕的示例,很抱歉,我尝试尽快更新我的问题!
#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