創(chuàng)新互聯(lián)公司主營萬山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,萬山h5成都微信小程序搭建,萬山網(wǎng)站營銷推廣歡迎萬山等地區(qū)企業(yè)咨詢
1,關(guān)于序列化.png 2,關(guān)于序列化.png 3,關(guān)于序列化.png 將結(jié)構(gòu)體寫入文件 name存在空格,C++的序列化相當(dāng)?shù)拇嗳?C++ 序列化 二進(jìn)制版 C風(fēng)格的 序列化,總是正常的 boost 序列化C風(fēng)格 寫入到文件simple boost 序列化C++風(fēng)格 寫入到文件simple boost 序列化從文件中讀取 非侵入式的 boost序列化 缺點(diǎn):所有的成員必須是public 可序列化的成員 派生類的序列化,問題引出,父類的成員增刪對序列化有影響.耦合度太高 派生類的序列化 問題解決 指針的序列化 兩個(gè)指針指向同一片區(qū)域,序列化到文件,只保存一份 智能指針的序列化 數(shù)組的序列化 指針數(shù)組的序列化 STL容器的序列化 將對象序列化為XML描述
將結(jié)構(gòu)體寫入文件
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分開,方便stream的讀入 return os<<user.name<<" "<<user.email<<" "<<user.age; } istream& operator>>(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.txt");//為了演示,省略檢查機(jī)制 fout<< user1 <<endl;//寫入到文件 fout<< user2 <<endl;//寫入到文件 User user3; User user4; ifstream fin("user.txt"); fin >> user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ > user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out chunli li_chunli@mail.com 24 林沖 林沖@email.com 30 chunli@Linux:~/boost$ cat user.txt chunli li_chunli@mail.com 24 林沖 林沖@email.com 30 chunli@Linux:~/boost$
name存在空格,C++的序列化相當(dāng)?shù)拇嗳?/strong>
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分開,方便stream的讀入 return os<<user.name<<" "<<user.email<<" "<<user.age; } istream& operator>>(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.txt");//為了演示,省略檢查機(jī)制 fout<< user1 <<endl;//寫入到文件 fout<< user2 <<endl;//寫入到文件 User user3; User user4; ifstream fin("user.txt"); fin >> user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ >user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out li chunli 0 335933440 chunli@Linux:~/boost$ cat user.txt li chunli li_chunli@mail.com 24 林沖 林沖@email.com 30 chunli@Linux:~/boost$
C++ 序列化 二進(jìn)制版
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.bin");//為了演示,省略檢查機(jī)制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; User user4; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.close(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -Wall -g && ./a.out li chunli,li_chunli@mail.com,24 林沖,林沖@email.com,30 *** Error in `./a.out': double free or corruption (fasttop): 0x00000000008a90b0 *** 已放棄 (核心已轉(zhuǎn)儲(chǔ)) chunli@Linux:~/boost$
C風(fēng)格的 序列化,總是正常的
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { char name[100]; char email[100]; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.bin");//為了演示,省略檢查機(jī)制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.close(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -Wall -g && ./a.out li chunli,li_chunli@mail.com,24 林沖,林沖@email.com,30 chunli@Linux:~/boost$
boost 序列化C風(fēng)格 寫入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { char name[16]; char email[64]; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機(jī)制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 16 -26 -103 -70 -27 -92 -102 -26 -104 -97 0 0 0 0 0 0 0 64 -27 -112 -76 -25 -108 -88 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 16 -24 -79 -71 -27 -83 -112 -27 -92 -76 0 0 0 0 0 0 0 64 -26 -98 -105 -27 -122 -78 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 chunli@Linux:~/boost$
boost 序列化C++風(fēng)格 寫入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #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;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機(jī)制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吳用@email.com 40 9 豹子頭 16 林沖@email.com 30 chunli@Linux:~/boost$
boost 序列化從文件中讀取
chunli@Linux:~/boost$ >user_info chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #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;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機(jī)制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星,吳用@email.com,40 豹子頭,林沖@email.com,30 chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吳用@email.com 40 9 豹子頭 16 林沖@email.com 30chunli@Linux:~/boost$
非侵入式的 boost序列化 缺點(diǎn):所有的成員必須是public
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; }; template<class Archive> void serialize(Archive &ar,User &user ,const unsigned version) { ar & user.name & user.email & user.age; } int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機(jī)制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星,吳用@email.com,40 豹子頭,林沖@email.com,30 chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吳用@email.com 40 9 豹子頭 16 林沖@email.com 30chunli@Linux:~/boost$ chunli@Linux:~/boost$
可序列化的成員
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #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() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; User user3 = {"及時(shí)雨","宋江@email.com",42}; User user4 = {"黑旋風(fēng)","李逵@email.com",37}; Group group; group.gid = "8080"; group.leader = user1; group.members.push_back(user2); group.members.push_back(user3); group.members.push_back(user4); //寫入到文件 ofstream fout("group_info"); boost::archive::text_oarchive oa(fout); oa << group; fout.close(); //從文件中讀取 Group group2; ifstream fin("group_info"); boost::archive::text_iarchive ia(fin); ia >> group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator<User>(cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator<User> (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吳用@email.com, 40 豹子頭, 林沖@email.com, 30 及時(shí)雨, 宋江@email.com, 42 黑旋風(fēng), 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吳用@email.com 40 0 0 3 0 9 豹子頭 16 林沖@email.com 30 9 及時(shí)雨 16 宋江@email.com 42 9 黑旋風(fēng) 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生類的序列化,問題引出,父類的成員增刪對序列化有影響.耦合度太高
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #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 VIP:public User { int level; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & email;//將成員序列化到IO ar & level; ar & name; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋風(fēng)"; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋風(fēng) likui@mail.com chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 14 likui@mail.com 100 9 黑旋風(fēng)chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生類的序列化 問題解決
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #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 VIP:public User { int level; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & boost::serialization::base_object<User>(*this);//將父類序列化,不再管父類有多少個(gè)成員 ar & level; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋風(fēng)"; vip1.age = 34; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; cout << vip2.age << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋風(fēng) likui@mail.com 34 chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 0 0 9 黑旋風(fēng) 14 likui@mail.com 34 100chunli@Linux:~/boost$ chunli@Linux:~/boost$
指針的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #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() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return name; } string getEmail()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return email; } int getAge()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return age; } private: 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; } }; int main() { User *user1 = new User("及時(shí)雨","songjiang@mail.com",37); ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; file.close(); delete user1; User *user2; ifstream fin("user.info"); //boost::archive::text_iarchive ia(fin); boost::archive::text_iarchive ia(fin); ia >> user2; cout <<"name="<<user2->getName()<<", email="<<user2->getEmail()<<", age="<< user2->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及時(shí)雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及時(shí)雨 18 songjiang@mail.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
兩個(gè)指針指向同一片區(qū)域,序列化到文件,只保存一份
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #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() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return name; } string getEmail()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return email; } int getAge()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return age; } private: 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; } }; int main() { User *user1 = new User("及時(shí)雨","songjiang@mail.com",37); User *user2 = user1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; file.close(); delete user1; User *user3; User *user4; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; ia >> user4; cout <<"name="<<user4->getName()<<", email="<<user4->getEmail()<<", age="<< user4->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及時(shí)雨, email=songjiang@mail.com, age=37 name=及時(shí)雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及時(shí)雨 18 songjiang@mail.com 37 0 0chunli@Linux:~/boost$ chunli@Linux:~/boost$ 問題:以指針序列化,以對象讀取,失敗
智能指針的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return name; } string getEmail()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return email; } int getAge()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return age; } private: 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; } }; int main() { User *user1 = new User("及時(shí)雨","songjiang@mail.com",37); User *user2 = user1; boost::shared_ptr<User> sp1(new User("花和尚", "魯智深@mail.com", 38)); boost::shared_ptr<User> sp2 = sp1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; oa << sp1; oa << sp2; file.close(); delete user1; User *user3; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; boost::shared_ptr<User> sp3; ia >> sp3; cout <<"name="<<sp3->getName()<<", email="<<sp3->getEmail()<<", age="<< sp3->getAge()<< endl; ia >> sp3; cout <<"name="<<sp3->getName()<<", email="<<sp3->getEmail()<<", age="<< sp3->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及時(shí)雨, email=songjiang@mail.com, age=37 name=及時(shí)雨, email=songjiang@mail.com, age=37 name=花和尚, email=魯智深@mail.com, age=38 name=花和尚, email=魯智深@mail.com, age=38 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及時(shí)雨 18 songjiang@mail.com 37 0 0 0 1 0 1 9 花和尚 18 魯智深@mail.com 38 0 1chunli@Linux:~/boost$ chunli@Linux:~/boost$
數(shù)組的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return name; } string getEmail()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return email; } int getAge()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return age; } private: 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; } }; int main() { User users[3] = { User("及時(shí)雨","songjiang@mail.com",37), User("武行者","wusong@mail.com",37), User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <<buf[0].getName()<<","<<buf[0].getEmail()<<","<< buf[0].getAge()<< endl; cout <<buf[1].getName()<<","<<buf[1].getEmail()<<","<< buf[1].getAge()<< endl; cout <<buf[2].getName()<<","<<buf[2].getEmail()<<","<< buf[2].getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及時(shí)雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 0 9 及時(shí)雨 18 songjiang@mail.com 37 9 武行者 15 wusong@mail.com 37 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
指針數(shù)組的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return name; } string getEmail()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return email; } int getAge()const //函數(shù)體內(nèi)不能對成員數(shù)據(jù)做任何改動(dòng) { return age; } private: 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; } }; int main() { User *users[3] = { new User("及時(shí)雨","songjiang@mail.com",37), new User("武行者","wusong@mail.com",37), new User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User *buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <<buf[0]->getName()<<","<<buf[0]->getEmail()<<","<< buf[0]->getAge()<< endl; cout <<buf[1]->getName()<<","<<buf[1]->getEmail()<<","<< buf[1]->getAge()<< endl; cout <<buf[2]->getName()<<","<<buf[2]->getEmail()<<","<< buf[2]->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及時(shí)雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 1 0 0 9 及時(shí)雨 18 songjiang@mail.com 37 0 1 9 武行者 15 wusong@mail.com 37 0 2 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
STL容器的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #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() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; User user3 = {"及時(shí)雨","宋江@email.com",42}; User user4 = {"黑旋風(fēng)","李逵@email.com",37}; Group group; group.gid = "8080"; group.leader = user1; group.members.push_back(user2); group.members.push_back(user3); group.members.push_back(user4); //寫入到文件 ofstream fout("group_info"); boost::archive::text_oarchive oa(fout); oa << group; fout.close(); //從文件中讀取 Group group2; ifstream fin("group_info"); boost::archive::text_iarchive ia(fin); ia >> group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator<User>(cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator<User> (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吳用@email.com, 40 豹子頭, 林沖@email.com, 30 及時(shí)雨, 宋江@email.com, 42 黑旋風(fēng), 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吳用@email.com 40 0 0 3 0 9 豹子頭 16 林沖@email.com 30 9 及時(shí)雨 16 宋江@email.com 42 9 黑旋風(fēng) 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
將對象序列化為XML描述
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/vector.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 & BOOST_SERIALIZATION_NVP(name); ar & BOOST_SERIALIZATION_NVP(email); ar & BOOST_SERIALIZATION_NVP(age); } }; ostream& operator<<(ostream& os, const User& u) { return os << u.name << ' ' << u.email << ' ' << u.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 & BOOST_SERIALIZATION_NVP(gid); ar & BOOST_SERIALIZATION_NVP(leader); ar & BOOST_SERIALIZATION_NVP(members); } }; int main() { Group group; group.gid = "G01"; User leader = { "tom", "tom@abc.com", 28 }; group.leader = leader; User u1 = { "tom1", "tom1@abc.com", 28 }; User u2 = { "tom2", "tom2@abc.com", 28 }; User u3 = { "tom3", "tom3@abc.com", 28 }; group.members.push_back(u1); group.members.push_back(u2); group.members.push_back(u3); ofstream fout("users_xml"); boost::archive::xml_oarchive oa(fout); oa << BOOST_SERIALIZATION_NVP(group); fout.close(); Group g2; ifstream fin("users_xml"); boost::archive::xml_iarchive ia(fin); ia >> BOOST_SERIALIZATION_NVP(g2); cout << g2.leader << endl; copy(g2.members.begin(), g2.members.end(), ostream_iterator<User> (cout,"\n")); } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out tom tom@abc.com 28 tom1 tom1@abc.com 28 tom2 tom2@abc.com 28 tom3 tom3@abc.com 28 chunli@Linux:~/boost$ cat users_xml <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="14"> <group class_id="0" tracking_level="0" version="0"> <gid>G01</gid> <leader class_id="1" tracking_level="0" version="0"> <name>tom</name> <email>tom@abc.com</email> <age>28</age> </leader> <members class_id="2" tracking_level="0" version="0"> <count>3</count> <item_version>0</item_version> <item> <name>tom1</name> <email>tom1@abc.com</email> <age>28</age> </item> <item> <name>tom2</name> <email>tom2@abc.com</email> <age>28</age> </item> <item> <name>tom3</name> <email>tom3@abc.com</email> <age>28</age> </item> </members> </group> chunli@Linux:~/boost$ chunli@Linux:~/boost$
網(wǎng)頁題目:7C++Boost序列化
網(wǎng)站URL:http://jinyejixie.com/article38/jjidpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站營銷、品牌網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、定制網(wǎng)站、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)