我能够使用boost序列化将我的数据保存到磁盘上。但是,我无法取回数据。你能告诉我我做错了什么吗?无法使用boost升级到磁盘后加载数据
谢谢
下面是我的代码
void nDB::save_macros(string filename) {
std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary);
//assert(ofs.good());
boost::archive::binary_oarchive oa(ofs);
oa << this;
}
void nDB::load_macros(string filename) {
std::ifstream ifs(filename.c_str());
//assert(ifs.good());
boost::archive::binary_iarchive ia(ifs);
nDB *db = new nDB;
ia >> db;
*this = *db;
}
下面是我的序列化实例
template<class Archive>
void nDB::serialize(Archive &ar, const unsigned int version) {
boost::unordered_map<string,macro*,myhash,cmp_str>::iterator M_IT;
boost::unordered_map<string,layer*,myhash,cmp_str>::iterator L_IT;
for (L_IT = _LAYERS.begin();L_IT != _LAYERS.end();L_IT++) {
string tmpstr = L_IT->first;
//ar & L_IT->first;
ar & tmpstr;
ar & *(L_IT->second);
}
for (M_IT = _MACROS.begin();M_IT != _MACROS.end();M_IT++) {
string tmpstr = M_IT->first;
//ar & M_IT->first;
ar & tmpstr;
ar & *(M_IT->second);
}
}
下面是我的运行结果是节省运行:
Insert macro mac1 into database OK!
Insert macro mac2 into database OK!
Insert macro mac3 into database OK!
Insert port P1 OK!
Insert port P2 OK!
Insert port P2 OK!
Insert port P3 OK!
Insert port P1 OK!
Insert port P3 OK!
Layer mac3 is found
Macro mac3 has these port:
Port P3 is found
Port P1 is found
Port P3 of macro mac3 has use CLOCK and dir INOUT
Port P1 of macro mac3 has use POWER and dir OUTPUT
以下是米y结果加载运行
ERROR:: Could not find macro mac3 in database
感谢Aldo,我实际上已经初始化了unordered_map序列化函数。我不知道你可以这样使用它。非常感谢您的建议。 – 2012-04-03 04:20:32
http://stackoverflow.com/questions/4287299/boostserialization-of-boostunordered-map – Pablo 2012-04-03 20:44:00