您的位置:首页 > 财经 > 产业 > 中国软件公司排名100强_网站建设与维护的内容_博客seo优化技术_企业策划方案怎么做

中国软件公司排名100强_网站建设与维护的内容_博客seo优化技术_企业策划方案怎么做

2025/4/19 16:40:55 来源:https://blog.csdn.net/weixin_54474317/article/details/147153830  浏览:    关键词:中国软件公司排名100强_网站建设与维护的内容_博客seo优化技术_企业策划方案怎么做
中国软件公司排名100强_网站建设与维护的内容_博客seo优化技术_企业策划方案怎么做

C++ Cereal 库使用指南

Cereal 是一个轻量级的 C++ 序列化库,用于将对象序列化为二进制、XML 或 JSON 格式,以及从这些格式反序列化。它支持标准库类型和用户自定义类型的序列化,且无需修改原有类定义。

基本用法

1. 安装与包含

#include <cereal/archives/binary.hpp>
#include <cereal/types/vector.hpp>  // 包含对vector的支持
#include <cereal/types/string.hpp> // 包含对string的支持

2. 基本序列化/反序列化

#include <fstream>
#include <iostream>
#include <vector>struct MyData {int x;double y;std::string z;std::vector<int> nums;// 序列化函数模板template<class Archive>void serialize(Archive & archive) {archive(x, y, z, nums); // 序列化所有成员}
};int main() {MyData data1{42, 3.14, "hello", {1, 2, 3}};// 序列化到文件{std::ofstream os("data.bin", std::ios::binary);cereal::BinaryOutputArchive archive(os);archive(data1); // 写入数据}// 从文件反序列化MyData data2;{std::ifstream is("data.bin", std::ios::binary);cereal::BinaryInputArchive archive(is);archive(data2); // 读取数据}std::cout << data2.x << ", " << data2.y << ", " << data2.z << std::endl;return 0;
}

高级特性

1. 分离加载和保存

struct MyClass {int a;std::string b;template<class Archive>void save(Archive & archive) const {archive(a, b);}template<class Archive>void load(Archive & archive) {archive(a, b);}
};

2. 版本控制

struct VersionedData {int x;std::string y;template<class Archive>void serialize(Archive & ar, const std::uint32_t version) {ar(x, y);if(version >= 2) {// 版本2新增的字段}}
};// 注册版本
CEREAL_CLASS_VERSION(VersionedData, 2)

3. 指针和智能指针

struct Node {int value;std::shared_ptr<Node> next;template<class Archive>void serialize(Archive & ar) {ar(value, next);}
};

4. 继承

struct Base {int x;template<class Archive>void serialize(Archive & ar) {ar(x);}
};struct Derived : Base {double y;template<class Archive>void serialize(Archive & ar) {ar(cereal::base_class<Base>(this), y);}
};

不同格式支持

1. 二进制格式

#include <cereal/archives/binary.hpp>// 输出
std::ofstream os("data.bin", std::ios::binary);
cereal::BinaryOutputArchive archive(os);
archive(data);// 输入
std::ifstream is("data.bin", std::ios::binary);
cereal::BinaryInputArchive archive(is);
archive(data);

2. JSON格式

#include <cereal/archives/json.hpp>// 输出
std::ofstream os("data.json");
cereal::JSONOutputArchive archive(os);
archive(cereal::make_nvp("MyData", data)); // 使用命名// 输入
std::ifstream is("data.json");
cereal::JSONInputArchive archive(is);
archive(data);

3. XML格式

#include <cereal/archives/xml.hpp>// 输出
std::ofstream os("data.xml");
cereal::XMLOutputArchive archive(os);
archive(cereal::make_nvp("MyData", data));// 输入
std::ifstream is("data.xml");
cereal::XMLInputArchive archive(is);
archive(data);

实际应用示例

1. 序列化自定义类

class Person {
public:Person() = default;Person(std::string n, int a) : name(n), age(a) {}private:std::string name;int age;std::vector<std::string> friends;friend class cereal::access;template<class Archive>void serialize(Archive & ar) {ar(name, age, friends);}
};

2. 序列化STL容器

std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}};
std::vector<double> myVec = {1.1, 2.2, 3.3};{std::ofstream os("container.bin", std::ios::binary);cereal::BinaryOutputArchive archive(os);archive(myMap, myVec);
}

3. 多态类型序列化

struct Base {virtual void foo() = 0;virtual ~Base() = default;template<class Archive>void serialize(Archive & ar) {}
};struct Derived : Base {int x;void foo() override {}template<class Archive>void serialize(Archive & ar) {ar(cereal::base_class<Base>(this), x);}
};// 注册多态类型
CEREAL_REGISTER_TYPE(Derived)

注意事项

  1. 默认构造函数:反序列化的类必须有可访问的默认构造函数

  2. 内存管理:序列化原始指针时需要特别小心,推荐使用智能指针

  3. 版本兼容性:修改类结构后,旧版本数据可能无法正确反序列化

  4. 性能:二进制格式通常比文本格式更高效

  5. 可移植性:二进制数据在不同平台间可能存在兼容性问题

Cereal 是一个强大而灵活的序列化库,通过简单的接口实现了复杂对象的序列化功能,非常适合需要持久化或网络传输C++对象的场景。

深入学习链接(参考🔗)C++序列化Cereal库的使用-CSDN博客

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com