概述
通过ostream/istream完成数据序列化,简单直接,学到此,做个记录。
std::ostream& operator<<(std::ostream& o, const Point& p) {
o << p.x << " " << p.y << " ";
return o;
}
std::istream& operator>>(std::istream& is, Point& p) {
is >> p.x;
is >> p.y;
return is;
}
std::ostream& operator<<(std::ostream& o, const Paths& paths) {
o << paths.source << paths.destinations.size() << " ";
for (const auto& x : paths.destinations) {
o << x;
}
return o;
}
std::istream& operator>>(std::istream& is, Paths& paths) {
size_t size;
is >> paths.source;
is >> size;
for (;size;size--) {
Point tmp;
is >> tmp;
paths.destinations.push_back(tmp);
}
return is;
}
测试代码
#include <iostream>
#include <sstream>
#include <list>
struct Point {
int x, y;
};
struct Paths {
Point source;
std::list<Point> destinations;
};
std::ostream& operator<<(std::ostream& o, const Point& p) {
o << p.x << " " << p.y << " ";
return o;
}
std::istream& operator>>(std::istream& is, Point& p) {
is >> p.x;
is >> p.y;
return is;
}
std::ostream& operator<<(std::ostream& o, const Paths& paths) {
o << paths.source << paths.destinations.size() << " ";
for (const auto& x : paths.destinations) {
o << x;
}
return o;
}
std::istream& operator>>(std::istream& is, Paths& paths) {
size_t size;
is >> paths.source;
is >> size;
for (;size;size--) {
Point tmp;
is >> tmp;
paths.destinations.push_back(tmp);
}
return is;
}
int main(int argc, char** argv) {
Paths paths = {{0, 0}, {{1, 1}, {0, 1}, {1, 0}}};
std::stringstream in;
in << paths;
std::string serialized = in.str();
std::cout << "Serialized paths into the string: ["
<< serialized << "]" << std::endl;
std::stringstream out(serialized);
Paths paths2;
out >> paths2;
std::cout << "Original: " << paths.destinations.size()
<< " destinations" << std::endl;
std::cout << "Restored: " << paths2.destinations.size()
<< " destinations" << std::endl;
return 0;
}
我公司承接各类技术服务,主要聚焦于:stm32、单片机、嵌入式、QT应用开发、Web+Python+Django应用开发。欢迎合作。