久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

你如何在 C++ 中序列化一個對象?

How do you serialize an object in C++?(你如何在 C++ 中序列化一個對象?)
本文介紹了你如何在 C++ 中序列化一個對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個小的對象層次結構,我需要通過套接字連接序列化和傳輸這些對象.我需要序列化對象,然后根據(jù)它的類型反序列化它.在 C++ 中有沒有一種簡單的方法可以做到這一點(就像在 Java 中一樣)?

是否有 C++ 序列化在線代碼示例或教程?

明確地說,我正在尋找將對象轉換為字節(jié)數(shù)組,然后再轉換回對象的方法.我可以處理套接字傳輸.

解決方案

談序列化,boost 序列化 API.至于通過網(wǎng)絡傳輸序列化數(shù)據(jù),我要么使用伯克利套接字,要么使用 asio 庫.>


如果要將對象序列化為字節(jié)數(shù)組,可以按以下方式使用 boost 序列化器(取自教程站點):

#include #include 類 gps_position{私人的:友元類 boost::serialization::access;模板<類存檔>void serialize(Archive & ar, const unsigned int version){和度;和分鐘;和秒;}整數(shù)度;整數(shù)分鐘;浮動秒;民眾:gps_position(){};gps_position(int d, int m, float s) :度(d)、分(m)、秒(s){}};

實際的序列化非常簡單:

#include std::ofstream ofs("filename.dat", std::ios::binary);//創(chuàng)建類實例const gps_position g(35, 59, 24.567f);//保存數(shù)據(jù)到存檔{boost::archive::binary_oarchive oa(ofs);//將類實例寫入存檔oa<<G;//調(diào)用析構函數(shù)時存檔和流關閉}

反序列化的工作方式類似.

還有一些機制可以讓您處理指針的序列化(復雜的數(shù)據(jù)結構,如 tres 等沒有問題),派生類,您可以在二進制和文本序列化之間進行選擇.此外,所有 STL 容器都是開箱即用的.

I have a small hierarchy of objects that I need to serialize and transmit via a socket connection. I need to both serialize the object, then deserialize it based on what type it is. Is there an easy way to do this in C++ (as there is in Java)?

Are there any C++ serialization online code samples or tutorials?

EDIT: Just to be clear, I'm looking for methods on converting an object into an array of bytes, then back into an object. I can handle the socket transmission.

解決方案

Talking about serialization, the boost serialization API comes to my mind. As for transmitting the serialized data over the net, I'd either use Berkeley sockets or the asio library.

Edit:
If you want to serialize your objects to a byte array, you can use the boost serializer in the following way (taken from the tutorial site):

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
class gps_position
{
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;

public:
    gps_position(){};
    gps_position(int d, int m, float s) :
    degrees(d), minutes(m), seconds(s)
    {}
};

Actual serialization is then pretty easy:

#include <fstream>
std::ofstream ofs("filename.dat", std::ios::binary);

    // create class instance
    const gps_position g(35, 59, 24.567f);

    // save data to archive
    {
        boost::archive::binary_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
        // archive and stream closed when destructors are called
    }

Deserialization works in an analogous manner.

There are also mechanisms which let you handle serialization of pointers (complex data structures like tress etc are no problem), derived classes and you can choose between binary and text serialization. Besides all STL containers are supported out of the box.

這篇關于你如何在 C++ 中序列化一個對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 免费性视频 | av在线影院 | 在线欧美亚洲 | 欧美精品被| 日韩久草 | 亚洲高清久久 | 天天干天天想 | 国产精品国产自产拍高清 | 91污在线| 国产成人精品一区二区三区 | 毛片一级片 | 国产一区二区 | 欧美黄色片| 国产精品一区二区三区久久久 | 国产精品久久国产精品99 | 日本中文在线视频 | 欧美精品一区二区三区在线播放 | 国产午夜精品视频 | 日本成人午夜影院 | 免费在线观看黄视频 | 成人不卡| www国产精 | av一区二区在线观看 | 中文字幕精品视频 | 久久久久久91香蕉国产 | 国产九九精品视频 | 欧美男人天堂 | 国产精品欧美精品日韩精品 | 国产成人自拍av | 欧美一级片在线播放 | 黑色丝袜三级在线播放 | 一区二区在线 | 亚洲成人国产综合 | 99久久精品国产一区二区三区 | 一级少妇女片 | 国产黄色在线观看 | 狠狠综合网 | 国产精品日韩欧美一区二区三区 | 成人一区二区在线 | 亚洲91精品| 亚洲日本成人 |