如何使用结构序列化映射
how to serialize map with struct
我正在尝试以结构作为键来提升序列化映射值,但是在编译代码时出现以下错误:
/usr/include/boost/serialization/access.hpp:116:11: error: ‘struct main(int, char**)::MyKey’ has no member named ‘serialize’
t.serialize(ar, file_version);
这是我正在使用的主要代码:
#include <ros/ros.h>
#include <map>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
int main (int argc, char** argv)
{
ros::init (argc, argv, "training");
ros::NodeHandle nh;
struct MyKey {
int d0, d1, d2, a0, b0, a1, b1, a2, b2;
bool operator < (const MyKey& o) const {
return std::tie(d0, d1, d2, a0, b0, a1, b1, a2, b2) < std::tie(o.d0, o.d1, o.d2, o.a0, o.b0, o.a1, o.b1, o.a2, o.b2);
}
};
struct MyValue {
int p0, p1, p2;
};
std::map<MyKey, MyValue> pobj;
std::ofstream s("obj_pattern"); boost::archive::text_oarchive oa(s);
for(int i=0;i<5000000;i++) {
pobj.insert({{i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8}, {i+9, i+10, i+11}});
oa << pobj;
}
return 0;
}
如何删除此错误?
如果要序列化用户定义类型,则需要将序列化函数模板添加到类中。在此方法中,您可以声明序列化/还原类的哪些数据成员。
由于无法为 MyKey 的本地类移动定义定义成员函数模板,因此 MyValue 退出主函数:
struct MyKey {
int d0, d1, d2, a0, b0, a1, b1, a2, b2;
bool operator < (const MyKey& o) const {
return std::tie(d0, d1, d2, a0, b0, a1, b1, a2, b2)
< std::tie(o.d0, o.d1, o.d2, o.a0, o.b0, o.a1, o.b1, o.a2, o.b2);
}
template<class Ar>
void serialize (Ar& ar, const unsigned int) {
ar & d0;
ar & d1;
// ditto
}
};
struct MyValue {
int p0, p1, p2;
template<class Ar>
void serialize(Ar& ar, const unsigned int) {
ar & p0;
ar & p1;
//
}
};
int main (int argc, char** argv)
{
//...
}
构建地图后,应仅调用oa << pobj;
一次:
for(int i=0;i<5000000;i++) {
pobj.insert({{i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8}, {i+9, i+10, i+11}});
}
oa << pobj;
相关文章:
- 内联映射初始化的动态atexit析构函数崩溃
- 如何在C++中序列化结构数据
- 序列化,没有库的整数,得到奇怪的结果
- 如何知道QDataStream不能反序列化某些内容
- 如何使用Python从C++中读取谷物序列化数据
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何使用结构序列化映射
- 使用分配器对哈希映射进行高效的序列化和反序列化
- 手动提升序列化多映射
- c++ boost MPI & threading - 序列化错误:地址未映射
- 序列化结构的STL映射
- Qt c++ 中反序列化映射的大小增加
- 如何使用boost::multiprecision::mpq_rational对具有模板类的映射进行反序列化
- 在c++中使用MessagePack反序列化异构映射
- Cocos2d-x 3.1序列化int到字符串映射
- Boost序列化映射与比较器
- Boost::serialization只序列化映射的键
- 如何为boost融合映射添加boost归档序列化支持
- 使用映射增强二进制序列化,并在中的序列化时加倍崩溃