C++数据序列化

概述

通过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应用开发。欢迎合作。

相关推荐

  1. 手撕代码: C++实现数据序列和反序列

    2024-03-16 05:36:01       29 阅读
  2. C# 中优雅的动态序列接口返回数据

    2024-03-16 05:36:01       40 阅读
  3. 序列和反序列、pytest-DDT数据驱动

    2024-03-16 05:36:01       50 阅读
  4. C++中序列和反序列

    2024-03-16 05:36:01       59 阅读
  5. c# Newtonsoft.Json 序列和反序列

    2024-03-16 05:36:01       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 05:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 05:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 05:36:01       82 阅读
  4. Python语言-面向对象

    2024-03-16 05:36:01       91 阅读

热门阅读

  1. 《网络安全法》关于数据出境的条款

    2024-03-16 05:36:01       35 阅读
  2. springboot基础配置

    2024-03-16 05:36:01       35 阅读
  3. k8s系列-kubectl 命令快速参考

    2024-03-16 05:36:01       35 阅读
  4. K8S CNI

    K8S CNI

    2024-03-16 05:36:01      38 阅读
  5. k8s中 容器、pod服务、svc服务 这几个的区别

    2024-03-16 05:36:01       38 阅读
  6. k8s admin 用户生成token

    2024-03-16 05:36:01       42 阅读
  7. 安装k8s集群

    2024-03-16 05:36:01       35 阅读
  8. 24计算机考研调剂 | 太原科技大学

    2024-03-16 05:36:01       43 阅读
  9. 【MySQL】mysqladmin、mysqlshow、mysqlcheck都是干嘛的?

    2024-03-16 05:36:01       40 阅读
  10. 【CSS】前端开发中的常见CSS样式问题解决方案

    2024-03-16 05:36:01       40 阅读
  11. 【构建工具】PostCSS快速配置

    2024-03-16 05:36:01       42 阅读