C++ 如何使用struct序列化映射
我正在尝试使用struct作为键对映射值进行boost序列化,但在编译代码时出现以下错误:C++ 如何使用struct序列化映射,c++,serialization,boost,C++,Serialization,Boost,我正在尝试使用struct作为键对映射值进行boost序列化,但在编译代码时出现以下错误: /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&
/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;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
ros::init(argc,argv,“培训”);
新罕布什尔州诺德汉德尔;
结构MyKey{
int d0、d1、d2、a0、b0、a1、b1、a2、b2;
布尔运算符<(常数MyKey&o)常数{
返回标准::tie(d0,d1,d2,a0,b0,a1,b1,a2,b2) 对于(int i=0;i如果要序列化用户定义的类型,则需要将serialize函数模板添加到类中。在此方法中,您可以说明序列化/还原了类的哪些数据成员
由于无法为本地类定义成员函数模板,请将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)
{
//...
}
struct MyKey{
int d0、d1、d2、a0、b0、a1、b1、a2、b2;
布尔运算符<(常数MyKey&o)常数{
返回标准::连接(d0、d1、d2、a0、b0、a1、b1、a2、b2)
您应该调用oa请发布一个。@DineshLama您需要在这个对象上定义输入流,创建boost::archive::text\u iarchive
并调用operator>>:std::map pobj;std::ifstream(“obj\u模式”);boost::archive::text\u iarchive oa(s);oa>>pobj;
。如果在同一个程序中写入/读取,请确保在执行读取操作之前关闭输出流。实际上,我想使用内存映射读取这些值。我尝试使用boost序列化,但遇到了问题。
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;