Boost 庫是一個由C/C++語言的開發(fā)者創(chuàng)建并更新維護(hù)的開源類庫,其提供了許多功能強大的程序庫和工具,用于開發(fā)高質(zhì)量、可移植、高效的C應(yīng)用程序。Boost庫可以作為標(biāo)準(zhǔn)C庫的后備,通常被稱為準(zhǔn)標(biāo)準(zhǔn)庫,是C標(biāo)準(zhǔn)化進(jìn)程的重要開發(fā)引擎之一。使用Boost庫可以加速C應(yīng)用程序的開發(fā)過程,提高代碼質(zhì)量和性能,并且可以適用于多種不同的系統(tǒng)平臺和編譯器。Boost庫已被廣泛應(yīng)用于許多不同領(lǐng)域的C++應(yīng)用程序開發(fā)中,如網(wǎng)絡(luò)應(yīng)用程序、圖像處理、數(shù)值計算、多線程應(yīng)用程序和文件系統(tǒng)處理等。
Boost庫提供了一組通用的數(shù)據(jù)序列化和反序列化庫,包括archive、text_oarchive、text_iarchive、xml_oarchive、xml_iarchive等。可用于許多數(shù)據(jù)類型的持久化和傳輸。使用這些庫,我們可以輕松地將各種數(shù)據(jù)類型序列化到文件或流中,并從文件或流中反序列化數(shù)據(jù)。
4.1 針對文本的序列化
文本序列化是將程序中的數(shù)據(jù)結(jié)構(gòu)以文本的形式進(jìn)行編碼并持久化的過程,以便在需要時可以進(jìn)行解碼并重新構(gòu)造出這個數(shù)據(jù)結(jié)構(gòu)。在實際開發(fā)中,我們經(jīng)常需要使用文本序列化技術(shù)來保存程序狀態(tài)、交換數(shù)據(jù)以及網(wǎng)絡(luò)傳輸?shù)取?/p>
Boost庫中提供了一組非常方便的序列化工具來處理各種類型的序列化,這些工具可以輕松地將數(shù)據(jù)從內(nèi)存中打包創(chuàng)建成字符串,反序列化則是反之。針對文本的序列化技術(shù)還可為數(shù)據(jù)結(jié)構(gòu)提供良好的兼容性,可以用于跨操作系統(tǒng)和語言的數(shù)據(jù)序列化。
#include <iostream>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
void txt_save(std::string path,std::string save_string)
{
std::ofstream ptr(path);
boost::archive::text_oarchive archive(ptr);
std::string string = save_string;
archive << string;
}
std::string txt_load(std::string path)
{
std::ifstream ptr(path);
boost::archive::text_iarchive iarchive(ptr);
std::string string;
iarchive >> string;
return string;
}
int main(int argc, char * argv[])
{
// 文本格式序列化與反序列化
std::string save = "hello lyshark \n";
txt_save("c://txt_save.txt",save);
std::string text_load = txt_load("c://txt_save.txt");
std::cout << "輸出字符串: " << text_load << std::endl;
system("pause");
return 0;
}
4.2 針對數(shù)組的序列化
針對數(shù)組的序列化是一種將數(shù)組數(shù)據(jù)結(jié)構(gòu)進(jìn)行持久化和傳輸?shù)男蛄谢夹g(shù),它可以將數(shù)組中的數(shù)據(jù)轉(zhuǎn)化為二進(jìn)制流,使得其可以被傳輸和存儲。在實際開發(fā)中,我們經(jīng)常需要進(jìn)行數(shù)組的序列化操作,以便在需要時可以恢復(fù)出該數(shù)組的數(shù)據(jù)。Boost庫中提供了一組非常方便的序列化工具,可以輕松地將數(shù)組從內(nèi)存中打包創(chuàng)建成字符串,反序列化則是反之。
在本節(jié)中,我們將重點介紹Boost庫中針對數(shù)組的序列化相關(guān)概念和用法,包括如何使用Boost.Serialization
進(jìn)行數(shù)組序列化和反序列化操作、如何定義自定義數(shù)組序列化函數(shù)、如何處理多維數(shù)組以及如何進(jìn)行特定數(shù)據(jù)類型的序列化等。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中針對數(shù)組的序列化技術(shù)的實際應(yīng)用,提高C++程序開發(fā)能力。
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
void array_save(std::string path,int *my_array,int count)
{
std::ofstream ptr(path);
boost::archive::text_oarchive archive(ptr);
std::vector<int> vect(my_array, my_array + count);
archive & BOOST_SERIALIZATION_NVP(vect);
}
void array_load(std::string path)
{
std::ifstream ptr(path);
boost::archive::text_iarchive iarchive(ptr);
std::vector<int> vect;
iarchive >> BOOST_SERIALIZATION_NVP(vect);
std::ostream_iterator<int> object(std::cout, " ");
std::copy(vect.begin(), vect.end(), object);
}
int main(int argc, char * argv[])
{
int my_array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
array_save("c://array_save.txt", my_array, 10);
array_load("c://array_save.txt");
system("pause");
return 0;
}
4.3 針對結(jié)構(gòu)體的序列化
針對結(jié)構(gòu)體的序列化是一種將結(jié)構(gòu)體數(shù)據(jù)類型進(jìn)行持久化和傳輸?shù)男蛄谢夹g(shù),它可以將結(jié)構(gòu)體中的數(shù)據(jù)轉(zhuǎn)化為二進(jìn)制流,使得其可以被傳輸和存儲。在實際開發(fā)中,我們經(jīng)常需要進(jìn)行結(jié)構(gòu)體的序列化操作,以便在需要時可以恢復(fù)出該結(jié)構(gòu)體的數(shù)據(jù)。Boost庫中提供了一組非常方便的序列化工具,可以輕松地將結(jié)構(gòu)體從內(nèi)存中打包創(chuàng)建成字符串,反序列化則是反之。
在本節(jié)中,我們將重點介紹Boost庫中針對結(jié)構(gòu)體的序列化相關(guān)概念和用法,包括如何使用Boost.Serialization進(jìn)行結(jié)構(gòu)體序列化和反序列化操作、如何定義自定義結(jié)構(gòu)體序列化函數(shù)、如何處理結(jié)構(gòu)體中的指針等。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中針對結(jié)構(gòu)體的序列化技術(shù)的實際應(yīng)用,提高C++程序開發(fā)能力。
#include <iostream>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
typedef struct MyDate
{
unsigned int m_day;
unsigned int m_month;
unsigned int m_year;
MyDate(int d, int m, int y)
{
m_day = d;
m_month = m;
m_year = y;
}
MyDate()
{
m_day = 0;
m_month = 0;
m_year = 0;
}
template<typename Archive>
void serialize(Archive& archive, const unsigned int version)
{
archive & BOOST_SERIALIZATION_NVP(m_day);
archive & BOOST_SERIALIZATION_NVP(m_month);
archive & BOOST_SERIALIZATION_NVP(m_year);
}
}MyDate;
void struct_save(std::string path,MyDate *ptr)
{
std::ofstream file(path);
boost::archive::text_oarchive oa(file);
// MyDate d(15, 8, 1947);
oa & BOOST_SERIALIZATION_NVP(*ptr);
}
MyDate struct_load(std::string path)
{
MyDate ref;
std::ifstream file(path);
boost::archive::text_iarchive ia(file);
ia >> BOOST_SERIALIZATION_NVP(ref);
return ref;
}
int main(int argc, char * argv[])
{
// 序列化
MyDate save_data(12, 7, 1997);
struct_save("c://archive.txt", &save_data);
// 反序列化
MyDate load_data;
load_data = struct_load("c://archive.txt");
std::cout << "反序列化: " << load_data.m_day << std::endl;
system("pause");
return 0;
}
4.4 嵌套結(jié)構(gòu)體的序列化
嵌套結(jié)構(gòu)體的序列化是一種將復(fù)雜數(shù)據(jù)類型進(jìn)行持久化和傳輸?shù)男蛄谢夹g(shù),它不僅可以序列化單一的結(jié)構(gòu)體,還可以將多個結(jié)構(gòu)體嵌套在一起進(jìn)行序列化。在實際開發(fā)中,我們經(jīng)常需要進(jìn)行嵌套結(jié)構(gòu)體的序列化操作,以便在需要時可以恢復(fù)出該結(jié)構(gòu)體的數(shù)據(jù)。
在本節(jié)中,我們將重點介紹Boost庫中針對嵌套結(jié)構(gòu)體的序列化相關(guān)概念和用法,包括如何使用Boost.Serialization
進(jìn)行嵌套結(jié)構(gòu)體序列化和反序列化操作、如何定義自定義嵌套結(jié)構(gòu)體序列化函數(shù)、如何處理結(jié)構(gòu)體中的指針等。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中針對嵌套結(jié)構(gòu)體的序列化技術(shù)的實際應(yīng)用,提高C++程序開發(fā)能力。
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
struct User
{
string name;
string email;
int age;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned version)
{
ar & name & email & age;
}
};
struct Group
{
string gid;
User leader;
vector<User> members;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned version)
{
ar & gid & leader & members;
}
};
ostream& operator<<(ostream& os, const User& user)
{
return os << user.name << ", " << user.email << ", " << user.age << endl;
}
int main(int argc, char * argv[])
{
User user1 = { "admin", "admin@email.com", 40 };
User user2 = { "guest", "guest@email.com", 30 };
User user3 = { "lyshark", "lyshark@email.com", 42 };
User user4 = { "root", "root@email.com", 37 };
Group group;
group.gid = "10001";
group.leader = user1;
group.members.push_back(user2);
group.members.push_back(user3);
group.members.push_back(user4);
// 序列化到文件
ofstream fout("c://save.txt");
boost::archive::text_oarchive oa(fout);
oa << group;
fout.close();
// 反序列化
Group group_load;
ifstream fin("c://save.txt");
boost::archive::text_iarchive ia(fin);
ia >> group_load;
cout << group_load.leader;
copy(group_load.members.begin(), group_load.members.end(), ostream_iterator<User>(cout));
system("pause");
return 0;
}
4.5 針對類的序列化
針對類的序列化是一種將類數(shù)據(jù)類型進(jìn)行持久化和傳輸?shù)男蛄谢夹g(shù),它可以將類中的數(shù)據(jù)轉(zhuǎn)化為二進(jìn)制流,使得其可以被傳輸和存儲。
在實際開發(fā)中,我們經(jīng)常需要進(jìn)行類的序列化操作,以便在需要時可以恢復(fù)出該類的數(shù)據(jù)。Boost庫中提供了一組非常方便的序列化工具,可以輕松地將類從內(nèi)存中打包創(chuàng)建成字符串,反序列化則是反之。
#include <iostream>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
class User
{
public:
User()
{
name = "";
email = "";
age = 0;
}
User(const string& _name, const string& _email, const int & _age)
{
name = _name;
email = _email;
age = _age;
}
string getName() const
{
return name;
}
string getEmail() const
{
return email;
}
int getAge() const
{
return age;
}
private:
string name;
string email;
int age;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned version)
{
archive & name & email & age;
}
};
int main(int argc, char * argv[])
{
User ptr[3] =
{
User("admin", "admin@lyshark.com", 22),
User("guest", "guest@lyshark.com", 24),
User("lyshark", "lyshark@lyshark.com", 44)
};
// 序列化到文件
ofstream file("c://save.txt");
boost::archive::text_oarchive oa(file);
oa << ptr;
file.close();
// 反序列化加載到類中
User buf[3];
ifstream file_in("c://save.txt");
boost::archive::text_iarchive ia(file_in);
ia >> buf;
cout << "姓名1: "<< buf[0].getName() << "," << "姓名2: " << buf[1].getName() << endl;
system("pause");
return 0;
}
4.6 序列化文本到字符串
將序列化文本轉(zhuǎn)換成字符串是序列化和反序列化過程中的一項常見需求,可以用于網(wǎng)絡(luò)傳輸、文件存儲等場景。Boost庫中提供了一組非常方便的序列化工具,可以將序列化文本打包成字符串,反序列化則是反之。
在本節(jié)中,我們將重點介紹如何將序列化文本轉(zhuǎn)換為字符串,包括如何將二進(jìn)制流進(jìn)行編碼、如何進(jìn)行限長編碼以及如何使用Boost.Serialization中的相關(guān)類進(jìn)行編碼操作等。此外,還會介紹如何進(jìn)行序列化和反序列化過程中的錯誤處理。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中序列化文本到字符串的技術(shù)實現(xiàn),提高C++程序開發(fā)能力。
#include <iostream>
#include <sstream>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
std::string binary_save(std::string save_string)
{
std::ostringstream os;
boost::archive::binary_oarchive archive(os);
archive << save_string;
std::string content = os.str();
return content;
}
std::string binary_load(std::string load_string)
{
std::istringstream is(load_string);
boost::archive::binary_iarchive archive(is);
std::string item;
archive >> item;
return item;
}
int main(int argc, char * argv[])
{
// 將字符串序列化,并存入get變量
std::string get = binary_save(std::string("hello lyshark"));
std::cout << "序列化后: " << get << std::endl;
std::string load = binary_load(get);
std::cout << "反序列化: " << load << std::endl;
system("pause");
return 0;
}
4.7 序列化數(shù)組到字符串
將序列化的數(shù)組數(shù)據(jù)轉(zhuǎn)換成字符串是序列化和反序列化過程中的一項常見需求,可以用于網(wǎng)絡(luò)傳輸、文件存儲等場景。Boost庫中提供了一組非常方便的序列化工具,可以將序列化的數(shù)組數(shù)據(jù)打包成字符串,反序列化則是反之。
在本節(jié)中,我們將重點介紹如何將序列化的數(shù)組轉(zhuǎn)換為字符串,包括如何將二進(jìn)制流進(jìn)行編碼、如何進(jìn)行限長編碼以及如何使用Boost.Serialization中的相關(guān)類進(jìn)行編碼操作等。此外,還會介紹如何進(jìn)行序列化和反序列化過程中的錯誤處理。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中序列化數(shù)組到字符串的技術(shù)實現(xiàn),提高C++程序開發(fā)能力。
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
std::string array_save(int *my_array, int count)
{
std::ostringstream os;
boost::archive::binary_oarchive archive(os);
std::vector<int> vect(my_array, my_array + count);
archive & BOOST_SERIALIZATION_NVP(vect);
std::string content = os.str();
return content;
}
std::vector<int> array_load(std::string load_string)
{
std::istringstream is(load_string);
boost::archive::binary_iarchive archive(is);
std::vector<int> vect;
archive >> BOOST_SERIALIZATION_NVP(vect);
return vect;
}
int main(int argc, char * argv[])
{
int my_array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
std::string str = array_save(my_array, 10);
std::cout << "序列化后: " << str << std::endl;
std::vector<int> vect = array_load(str);
for (int x = 0; x < 10; x++)
{
std::cout << "反序列化輸出: " << vect[x] << std::endl;
}
system("pause");
return 0;
}
4.8 序列化結(jié)構(gòu)體到字符串
將序列化的結(jié)構(gòu)體數(shù)據(jù)轉(zhuǎn)換成字符串是序列化和反序列化過程中的一項常見需求,可以用于網(wǎng)絡(luò)傳輸、文件存儲等場景。Boost庫中提供了一組非常方便的序列化工具,可以將序列化的結(jié)構(gòu)體數(shù)據(jù)打包成字符串,反序列化則是反之。在本節(jié)中,我們將重點介紹如何將序列化的結(jié)構(gòu)體數(shù)據(jù)轉(zhuǎn)換為字符串,包括如何將二進(jìn)制流進(jìn)行編碼、如何進(jìn)行限長編碼、基于文本的序列化操作以及如何使用Boost.Serialization
中的相關(guān)類進(jìn)行編碼操作等。
此外,還會介紹如何進(jìn)行序列化和反序列化過程中的錯誤處理。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中序列化結(jié)構(gòu)體到字符串的技術(shù)實現(xiàn),提高C++程序開發(fā)能力。
#include <iostream>
#include <sstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
typedef struct MyDate
{
unsigned int m_day;
unsigned int m_month;
unsigned int m_year;
MyDate(int d, int m, int y)
{
m_day = d;
m_month = m;
m_year = y;
}
MyDate()
{
m_day = 0;
m_month = 0;
m_year = 0;
}
template<typename Archive>
void serialize(Archive& archive, const unsigned int version)
{
archive & BOOST_SERIALIZATION_NVP(m_day);
archive & BOOST_SERIALIZATION_NVP(m_month);
archive & BOOST_SERIALIZATION_NVP(m_year);
}
}MyDate;
std::string struct_save(MyDate *ptr)
{
std::ostringstream os;
boost::archive::binary_oarchive archive(os);
archive & BOOST_SERIALIZATION_NVP(*ptr);
std::string content = os.str();
return content;
}
MyDate struct_load(std::string load_string)
{
MyDate item;
std::istringstream is(load_string);
boost::archive::binary_iarchive archive(is);
archive >> item;
return item;
}
int main(int argc, char * argv[])
{
// 序列化
MyDate save_data(12, 7, 1997);
std::string save_string = struct_save(&save_data);
std::cout << "序列化后: " << save_string << std::endl;
// 反序列化
MyDate ptr;
ptr = struct_load(save_string);
std::cout << "反序列化: " << ptr.m_year << std::endl;
system("pause");
return 0;
}
4.9 序列化嵌套結(jié)構(gòu)到字符串
將嵌套結(jié)構(gòu)序列化數(shù)據(jù)轉(zhuǎn)換成字符串是序列化和反序列化過程中的一項常見需求,可以用于網(wǎng)絡(luò)傳輸、文件存儲等場景。Boost庫中提供了一組非常方便的序列化工具,可以將序列化的嵌套結(jié)構(gòu)數(shù)據(jù)打包成字符串,反序列化則是反之。
在本節(jié)中,我們將重點介紹如何將序列化的嵌套結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換為字符串,包括如何將二進(jìn)制流進(jìn)行編碼、如何進(jìn)行限長編碼、基于文本的序列化操作以及如何使用Boost.Serialization
中的相關(guān)類進(jìn)行編碼操作等。此外,還會介紹如何進(jìn)行序列化和反序列化過程中的錯誤處理。通過本節(jié)的學(xué)習(xí),讀者可掌握Boost庫中序列化嵌套結(jié)構(gòu)到字符串的技術(shù)實現(xiàn),提高C++程序開發(fā)能力。
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
struct User
{
string name;
string email;
int age;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned version)
{
ar & name & email & age;
}
};
struct Group
{
string gid;
User leader;
vector<User> members;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned version)
{
ar & gid & leader & members;
}
};
// 序列化
std::string struct_save(Group *ptr)
{
std::ostringstream os;
boost::archive::binary_oarchive archive(os);
archive & BOOST_SERIALIZATION_NVP(*ptr);
std::string content = os.str();
return content;
}
// 反序列化
Group struct_load(std::string load_string)
{
Group item;
std::istringstream is(load_string);
boost::archive::binary_iarchive archive(is);
archive >> item;
return item;
}
int main(int argc, char * argv[])
{
User user1 = { "admin", "admin@email.com", 40 };
User user2 = { "guest", "guest@email.com", 30 };
User user3 = { "lyshark", "lyshark@email.com", 42 };
User user4 = { "root", "root@email.com", 37 };
Group group;
group.gid = "10001";
group.leader = user1;
group.members.push_back(user2);
group.members.push_back(user3);
group.members.push_back(user4);
// 序列化
std::string save = struct_save(&group);
std::cout << "序列化后: " << save << std::endl;
// 反序列化
Group load;
load = struct_load(save);
std::cout << "UUID: " << load.gid << std::endl;
std::cout << "Uname: " << load.members[0].name << std::endl;
std::cout << "Uname2: " << load.members[1].name << std::endl;
system("pause");
return 0;
}
4.10 序列化類到字符串
在本節(jié)中,我們將重點介紹如何將序列化的類數(shù)據(jù)轉(zhuǎn)換為字符串,包括如何將二進(jìn)制流進(jìn)行編碼、如何進(jìn)行限長編碼、基于文本的序列化操作以及如何使用Boost.Serialization中的相關(guān)類進(jìn)行編碼操作等。此外,還會介紹如何進(jìn)行序列化和反序列化過程中的錯誤處理。文章來源:http://www.zghlxwxcb.cn/news/detail-661503.html
#include <iostream>
#include <sstream>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
class User
{
public:
User()
{
name = "";
email = "";
age = 0;
}
User(const string& _name, const string& _email, const int & _age)
{
name = _name;
email = _email;
age = _age;
}
string getName() const
{
return name;
}
string getEmail() const
{
return email;
}
int getAge() const
{
return age;
}
private:
string name;
string email;
int age;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned version)
{
archive & name & email & age;
}
};
int main(int argc, char * argv[])
{
User ptr[3] =
{
User("admin", "admin@lyshark.com", 22),
User("guest", "guest@lyshark.com", 24),
User("lyshark", "lyshark@lyshark.com", 44)
};
// 序列化數(shù)據(jù)
std::ostringstream os;
boost::archive::binary_oarchive archive_save(os);
archive_save & BOOST_SERIALIZATION_NVP(ptr);
std::string content = os.str();
std::cout << content << std::endl;
// 返序列化
User item[3];
std::istringstream is(content);
boost::archive::binary_iarchive archive_load(is);
archive_load >> item;
cout << "姓名1: " << item[0].getName() << "," << "姓名2: " << item[1].getName() << endl;
system("pause");
return 0;
}
4.11 序列化派生類到字符串
將序列化的派生類數(shù)據(jù)轉(zhuǎn)換成字符串是序列化和反序列化過程中的一項常見需求,在本節(jié)中,我們將重點介紹如何將序列化的派生類數(shù)據(jù)轉(zhuǎn)換為字符串,包括如何將二進(jìn)制流進(jìn)行編碼、如何進(jìn)行限長編碼、基于文本的序列化操作以及如何使用Boost.Serialization
中的相關(guān)類進(jìn)行編碼操作等。文章來源地址http://www.zghlxwxcb.cn/news/detail-661503.html
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
struct User
{
string name;
string email;
int age;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned version)
{
ar & name & email & age;
}
};
struct Group :public User
{
int level;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned version)
{
// 將父類序列化,不管父類有多少個成員
archive & boost::serialization::base_object<User>(*this);
archive & level;
}
};
// 序列化
std::string struct_save(Group *ptr)
{
std::ostringstream os;
boost::archive::binary_oarchive archive(os);
archive & BOOST_SERIALIZATION_NVP(*ptr);
std::string content = os.str();
return content;
}
// 反序列化
Group struct_load(std::string load_string)
{
Group item;
std::istringstream is(load_string);
boost::archive::binary_iarchive archive(is);
archive >> item;
return item;
}
int main(int argc, char * argv[])
{
Group group_ptr;
group_ptr.name = "lyshark";
group_ptr.age = 24;
group_ptr.email = "me@lyshark.com";
group_ptr.level = 1024;
// 序列化到字符串
std::string save = struct_save(&group_ptr);
std::cout << "序列化后: " << save << std::endl;
// 反序列化到字符串
Group load;
load = struct_load(save);
std::cout << "名字: " << load.name << "序號: " << load.level << std::endl;
system("pause");
return 0;
}
到了這里,關(guān)于4.4 C++ Boost 數(shù)據(jù)集序列化庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!