您正在尝试做的是序列化。这种序列化对象的方式不稳定,很大程度上取决于机场是什么。最好使用显式序列化。
下面是对什么是序列化以及为什么采用这种方式的描述。
在MessagePack中,典型的序列化-反序列化场景如下所示:
struct airport {
std::string name; //you can name your airports here
int planeCapacity;
int acceptPlanesFrom;
MSGPACK_DEFINE(name,planeCapacity,acceptPlanesFrom);
};
...
// define your airports
std::vector<airport> airports;
airport a={"BLA",1,2};
airport b={"BLB",3,4};
airports.push_back(a);
airports.push_back(b);
// create a platform-independent byte sequence from your data
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, airports) ;
std::string data=sbuf.data();//you can write that into a file
msgpack::unpacked msg;
// get your data safely back
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
std::cout<<obj<<std::endl;
// now convert the bytes back to your objects
std::vector<airport> read_airports;
obj.convert(&read_airports);
std::cout<<read_airports.size()<<std::endl;
使用控制台输出:
[["BLA", 1, 2], ["BLB", 3, 4]]
2