2015-12-08 13 views
5

Ich versuche, eine STL-Karte zu serialisieren, wo der Schlüssel und der Wert Strukturen sind. Der angehängte Code funktioniert einwandfrei, wenn der Schlüssel eine Struktur ist und der Wert ein int ist. Ich habe jedoch Probleme beim Einrichten der Serialisierungsfunktion, um einen Strukturwert zu behandeln. Irgendwelche Vorschläge, wie man das macht?Serialisierung einer STL-Karte von Strukturen

Vielen Dank - Andrew.

#include <string> 
#include <fstream> 
#include <algorithm> 
#include <map> 
#include <unordered_map> 
#include <iostream> 
#include <chrono> 
#include <sstream> 
#include <functional> 

#include <boost/filesystem.hpp> 
#include <boost/filesystem/fstream.hpp> 
#include <boost/serialization/utility.hpp> 
#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 
#include <boost/serialization/string.hpp> 
#include <boost/serialization/map.hpp> 
#include <boost/serialization/unordered_map.hpp> 
#include <boost/serialization/version.hpp> 

using namespace std; 

struct portfolio_data { 
    double a; 
}; 

struct portfolio_key { 
    long id; 
    string name; 
}; 

struct portfolio_compare 
{ 
    bool operator() (portfolio_key k1, portfolio_key k2) const 
    { 
     return (
        (k1.id < k2.id) || 
        ((k1.id == k2.id) && (k1.name < k2.name)) 
       ); 
    } 
}; 

typedef map<portfolio_key, int, portfolio_compare> portfolio_map; 

//------------------------------------------------------------------------ 

template<class Archive> 
void serialize(Archive& ar, portfolio_key& key, const unsigned int version) 
{ 
    ar & boost::serialization::make_nvp("id", key.id); 
    ar & boost::serialization::make_nvp("name", key.name); 
} 

//------------------------------------------------------------------------ 

void save(portfolio_map& map_var, const std::string& file_name) 
{ 
    ofstream ofs(file_name.c_str(), ios::out | ios::binary); 
    boost::archive::binary_oarchive ba(ofs); // works for text too 
    ba << BOOST_SERIALIZATION_NVP(map_var); 
} 

//------------------------------------------------------------------------ 

portfolio_map load(const std::string& file_name) 
{ 
    portfolio_map map_var; 
    ifstream ifs(file_name.c_str(), ios::in | ios::binary); 
    boost::archive::binary_iarchive ba(ifs); 
    ba >> BOOST_SERIALIZATION_NVP(map_var); 
    return map_var; 
} 

//------------------------------------------------------------------------ 

int main(void) 
{ 
    portfolio_map map1; 

    for (long i = 0; i < 10; ++i) 
    { 
     portfolio_key k; 
     k.id = i; 
     k.name = "AAAAA"; 

     portfolio_data d; 
     map1[k] = i; 
    } 

    save(map1, "map.bin"); 

    portfolio_map map2; 
    map2 = load("map.bin"); 

    cout << map2.size() << endl; 
} 
+0

was ist, wenn Sie separate Schlüssel pro Datenelement der Struktur erstellen? zB um portfolio_key zu serialisieren, haben Sie Schlüssel "key-id" und Schlüssel "key-name". –

Antwort

1

Sie haben vergessen, nur serialize für Ihren Wert struct zu definieren, das ist alles:

template<class Archive> 
void serialize(Archive& ar, portfolio_data& value, const unsigned int version) 
{ 
    ar & boost::serialization::make_nvp("a", value.a); 
}