一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

C++在Linux中如何实现序列化

时间:2026-07-20 09:15:02 编辑:袖梨 来源:一聚教程网

在Linux中,C++实现序列化有多种方法,以下是一些常用的库和方法:

C++如何在Linux中实现序列化

1. 使用Boost.Serialization

Boost.Serialization是一个非常强大的库,支持多种数据类型的序列化和反序列化。

安装Boost库

首先,你需要安装Boost库。在大多数Linux发行版中,可以使用包管理器安装:

sudo apt-get install libboost-all-dev

示例代码

#include <boost/archive/text_oarchive.hpp>#include <boost/archive/text_iarchive.hpp>#include <boost/serialization/vector.hpp>#include <fstream>#include <iostream>#include <vector>class Person {public:std::string name;int age;template<class Archive>void serialize(Archive & ar, const unsigned int version) {ar & name;ar & age;}};int main() {std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}};// 序列化到文件{std::ofstream ofs("people.txt");boost::archive::text_oarchive oa(ofs);oa << people;}// 从文件反序列化std::vector<Person> loaded_people;{std::ifstream ifs("people.txt");boost::archive::text_iarchive ia(ifs);ia >> loaded_people;}// 打印反序列化后的数据for (const auto& person : loaded_people) {std::cout << person.name << " " << person.age << std::endl;}return 0;}

2. 使用Cereal

Cereal是一个轻量级的C++序列化库,易于使用且性能良好。

安装Cereal库

你可以从GitHub上下载Cereal库并手动编译安装:

git clone https://github.com/USCiLab/cereal.gitcd cerealmkdir build && cd buildcmake ..makesudo make install

示例代码

#include <cereal/archives/json.hpp>#include <cereal/types/vector.hpp>#include <fstream>#include <iostream>#include <vector>class Person {public:std::string name;int age;template<class Archive>void serialize(Archive & archive) {archive(name, age);}};int main() {std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}};// 序列化到文件{std::ofstream ofs("people.json");cereal::JSONOutputArchive oarchive(ofs);oarchive(people);}// 从文件反序列化std::vector<Person> loaded_people;{std::ifstream ifs("people.json");cereal::JSONInputArchive iarchive(ifs);iarchive(loaded_people);}// 打印反序列化后的数据for (const auto& person : loaded_people) {std::cout << person.name << " " << person.age << std::endl;}return 0;}

3. 使用标准库(如std::ofstream和std::ifstream)

如果你只需要简单的文本或二进制序列化,可以使用标准库中的文件流。

示例代码(文本序列化)

#include <fstream>#include <iostream>#include <vector>#include <string>struct Person {std::string name;int age;};int main() {std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}};// 序列化到文件{std::ofstream ofs("people.txt");for (const auto& person : people) {ofs << person.name << "," << person.age << std::endl;}}// 从文件反序列化std::vector<Person> loaded_people;{std::ifstream ifs("people.txt");std::string line;while (std::getline(ifs, line)) {size_t pos = line.find(',');Person person;person.name = line.substr(0, pos);person.age = std::stoi(line.substr(pos + 1));loaded_people.push_back(person);}}// 打印反序列化后的数据for (const auto& person : loaded_people) {std::cout << person.name << " " << person.age << std::endl;}return 0;}

示例代码(二进制序列化)

#include <fstream>#include <iostream>#include <vector>#include <string>struct Person {std::string name;int age;};int main() {std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}};// 序列化到文件{std::ofstream ofs("people.bin", std::ios::binary);for (const auto& person : people) {size_t name_size = person.name.size();ofs.write(reinterpret_cast<const char*>(&name_size), sizeof(name_size));ofs.write(person.name.c_str(), name_size);ofs.write(reinterpret_cast<const char*>(&person.age), sizeof(person.age));}}// 从文件反序列化std::vector<Person> loaded_people;{std::ifstream ifs("people.bin", std::ios::binary);while (ifs.good()) {Person person;size_t name_size;ifs.read(reinterpret_cast<char*>(&name_size), sizeof(name_size));person.name.resize(name_size);ifs.read(&person.name[0], name_size);ifs.read(reinterpret_cast<char*>(&person.age), sizeof(person.age));loaded_people.push_back(person);}}// 打印反序列化后的数据for (const auto& person : loaded_people) {std::cout << person.name << " " << person.age << std::endl;}return 0;}

这些方法各有优缺点,选择哪种方法取决于你的具体需求,例如性能、易用性和兼容性。

热门栏目