当前位置: 首页 > news >正文

网站备案经验百度今日排行榜

网站备案经验,百度今日排行榜,wordpress导入xml一直等待响应,福州网站建设的公司一、C 中的序列化和反序列化 (一)基本概念 在 C 中,序列化是将对象转换为字节流的过程,反序列化则是从字节流重新构建对象的过程。这对于存储对象状态到文件、网络传输等场景非常有用。 (二)简单的序列化…

一、C++ 中的序列化和反序列化

(一)基本概念

在 C++ 中,序列化是将对象转换为字节流的过程,反序列化则是从字节流重新构建对象的过程。这对于存储对象状态到文件、网络传输等场景非常有用。

(二)简单的序列化和反序列化实现方式

1. 基于文本格式

  • 序列化
    • 一种简单的方法是将对象的成员变量以特定的格式(如 CSV - 逗号分隔值)写入文件。例如,假设有一个Person类,包含姓名和年龄两个成员变量。
      #include <iostream>
      #include <fstream>
      #include <string>


      class Person {
      public:
          std::string name;
          int age;
          Person(const std::string& n, int a) : name(n), age(a) {}
      };


      void serializeToText(const Person& p, const std::string& filename) {
          std::ofstream file(filename);
          if (file.is_open()) {
              file << p.name << "," << p.age << std::endl;
              file.close();
          } else {
              std::cerr << "无法打开文件进行序列化。" << std::endl;
          }
      }
  • 反序列化
    Person deserializeFromText(const std::string& filename) {
        std::ifstream file(filename);
        Person p("", 0);
        if (file.is_open()) {
            std::string line;
            if (std::getline(file, line)) {
                size_t pos = line.find(',');
                if (pos!= std::string::npos) {
                    p.name = line.substr(0, pos);
                    p.age = std::stoi(line.substr(pos + 1));
                }
            }
            file.close();
        } else {
            std::cerr << "无法打开文件进行反序列化。" << std::endl;
        }
        return p;
    }

2. 使用二进制格式(更高效,但不可读)

  • 序列化
    • 使用fstream库的二进制模式来写入对象的内存表示。不过需要注意字节序等问题。
      void serializeToBinary(const Person& p, const std::string& filename) {
          std::ofstream file(filename, std::ios::binary);
          if (file.is_open()) {
              // 先写入姓名长度
              int nameLength = p.name.length();
              file.write(reinterpret_cast<const char*>(&nameLength), sizeof(int));
              // 再写入姓名内容
              file.write(p.name.c_str(), nameLength);
              // 写入年龄
              file.write(reinterpret_cast<const char*>(&p.age), sizeof(int));
              file.close();
          } else {
              std::cerr << "无法打开文件进行二进制序列化。" << std::endl;
          }
      }
  • 反序列化
    Person deserializeFromBinary(const std::string& filename) {
        std::ifstream file(filename, std::ios::binary);
        Person p("", 0);
        if (file.is_open()) {
            // 先读取姓名长度
            int nameLength;
            file.read(reinterpret_cast<char*>(&nameLength), sizeof(int));
            char* buffer = new char[nameLength + 1];
            // 读取姓名内容
            file.read(buffer, nameLength);
            buffer[nameLength] = '\0';
            p.name = buffer;
            delete[] buffer;
            // 读取年龄
            file.read(reinterpret_cast<char*>(&p.age), sizeof(int));
            file.close();
        } else {
            std::cerr << "无法打开文件进行二进制反序列化。" << std::endl;
        }
        return p;
    }

(三)使用第三方库(如 Boost.Serialization)进行序列化和反序列化

  • 安装和配置 Boost 库
    • 首先需要下载并安装 Boost 库,这通常涉及从 Boost 官方网站获取源代码,然后通过编译安装到系统中。安装过程因操作系统而异。
  • 使用示例
    • 以下是使用 Boost.Serialization 对Person类进行序列化和反序列化的简单示例。

      #include <iostream>
      #include <fstream>
      #include <boost/archive/text_oarchive.hpp>
      #include <boost/archive/text_iarchive.hpp>
      #include <boost/serialization/string.hpp>


      class Person {
      public:
          std::string name;
          int age;
          Person(const std::string& n, int a) : name(n), age(a) {}
          // 为了让Boost.Serialization能够访问私有成员,需要添加这两个友元函数
          friend class boost::serialization::access;
          template<class Archive>
          void serialize(Archive& ar, const unsigned int version) {
              ar & name;
              ar & age;
          }
      };


      void serializeWithBoost(const Person& p, const std::string& filename) {
          std::ofstream file(filename);
          if (file.is_open()) {
              boost::archive::text_oarchive oa(file);
              oa << p;
              file.close();
          } else {
              std::cerr << "无法打开文件进行Boost序列化。" << std::endl;
          }
      }


      Person deserializeWithBoost(const std::string& filename) {
          std::ifstream file(filename);
          Person p("", 0);
          if (file.is_open()) {
              boost::archive::text_iarchive ia(file);
              ia >> p;
              file.close();
          } else {
              std::cerr << "无法打开文件进行Boost反序列化。" << std::endl;
          }
          return p;
      }

二、C++ 中的对象生命周期管理

(一)对象生命周期阶段

  • 创建
    • 栈对象:在函数内部定义的对象,当程序执行到对象定义处时,会调用对象的构造函数进行创建。例如Person p("Alice", 30);,这里p是一个栈对象,它的生命周期从定义处开始,到所在的代码块结束(如函数返回)。
    • 堆对象:通过new关键字在堆上分配内存创建对象。例如Person* p = new Person("Bob", 25);,此时需要手动管理内存,使用delete来释放内存。
  • 使用:在对象的生命周期内,可以通过对象的成员函数访问和修改对象的状态,就像p->getName()p.setName("Charlie");这样的操作。
  • 销毁
    • 栈对象:当栈对象所在的代码块结束时,对象的析构函数会被自动调用,用于清理对象占用的资源。
    • 堆对象:需要手动调用delete来释放堆上对象占用的内存,并且在调用delete后,对象的析构函数会被调用。如果忘记调用delete,就会导致内存泄漏。

(二)内存管理和资源清理

  • RAII(Resource Acquisition Is Initialization)
    • 这是 C++ 中一种重要的资源管理机制。核心思想是将资源的获取和初始化放在构造函数中,将资源的释放放在析构函数中。例如,使用std::unique_ptrstd::shared_ptr来管理动态分配的内存。
    • // 不需要手动释放内存,当p离开作用域时,Person对象会被自动销毁
    • std::shared_ptr是共享所有权的智能指针,用于多个对象共享同一个资源的情况。例如,多个对象可能需要共享对同一个数据库连接对象的引用。
      手动内存管理(使用
      newdelete
    • 当使用new在堆上分配内存时,必须在适当的时候使用delete来释放内存。错误地使用delete(如多次删除同一个对象或者使用已经删除的对象)会导致程序出错,如段错误等。并且如果在delete之后还尝试访问对象成员,也是未定义行为。

文章转载自:
http://dinncocaaba.knnc.cn
http://dinncosleepless.knnc.cn
http://dinncoadaxial.knnc.cn
http://dinncorainbelt.knnc.cn
http://dinncoitt.knnc.cn
http://dinncodaystar.knnc.cn
http://dinncodesuetude.knnc.cn
http://dinncotrimethadione.knnc.cn
http://dinncohydrolyze.knnc.cn
http://dinncobop.knnc.cn
http://dinncoemblematize.knnc.cn
http://dinncougly.knnc.cn
http://dinncoprimp.knnc.cn
http://dinncoalienee.knnc.cn
http://dinncoptochocracy.knnc.cn
http://dinncopodophyllum.knnc.cn
http://dinncocounterrotation.knnc.cn
http://dinncorunnerless.knnc.cn
http://dinncofeasibility.knnc.cn
http://dinncophotopile.knnc.cn
http://dinncotriplicate.knnc.cn
http://dinncoorthodontia.knnc.cn
http://dinncotortrix.knnc.cn
http://dinncosanskrit.knnc.cn
http://dinncoritualism.knnc.cn
http://dinncoboa.knnc.cn
http://dinncoparvulus.knnc.cn
http://dinncorecapitulation.knnc.cn
http://dinncounrepair.knnc.cn
http://dinncomalajustment.knnc.cn
http://dinncobarnard.knnc.cn
http://dinncoprincipled.knnc.cn
http://dinncosnowberry.knnc.cn
http://dinncoacrimoniously.knnc.cn
http://dinncowaveless.knnc.cn
http://dinncoboyg.knnc.cn
http://dinncorepeatable.knnc.cn
http://dinncounredeemable.knnc.cn
http://dinncoterrazzo.knnc.cn
http://dinncolightish.knnc.cn
http://dinncoamy.knnc.cn
http://dinncoliechtenstein.knnc.cn
http://dinncoendoradiosonde.knnc.cn
http://dinncodps.knnc.cn
http://dinncoslimy.knnc.cn
http://dinncoproteinuria.knnc.cn
http://dinncoadenoidal.knnc.cn
http://dinncoindoctrinatory.knnc.cn
http://dinncoboatbill.knnc.cn
http://dinncoaccreditation.knnc.cn
http://dinncoplimsolls.knnc.cn
http://dinncogibbed.knnc.cn
http://dinncojibaro.knnc.cn
http://dinncopharyngitis.knnc.cn
http://dinncodisfranchisement.knnc.cn
http://dinncorecede.knnc.cn
http://dinncothermoperiodicity.knnc.cn
http://dinncocoastel.knnc.cn
http://dinncounsavory.knnc.cn
http://dinncosemiweekly.knnc.cn
http://dinncoaquamanile.knnc.cn
http://dinncotankerman.knnc.cn
http://dinncomiraculous.knnc.cn
http://dinncosynarthrodia.knnc.cn
http://dinncocartful.knnc.cn
http://dinncogreenbug.knnc.cn
http://dinncobez.knnc.cn
http://dinncoflowerage.knnc.cn
http://dinncofawningly.knnc.cn
http://dinncopentagonese.knnc.cn
http://dinncoomniscient.knnc.cn
http://dinncorichen.knnc.cn
http://dinncohydroacoustic.knnc.cn
http://dinncoama.knnc.cn
http://dinncoephemerae.knnc.cn
http://dinncolinearise.knnc.cn
http://dinncoreverently.knnc.cn
http://dinncomarlite.knnc.cn
http://dinncocultureless.knnc.cn
http://dinncounderearth.knnc.cn
http://dinncogalvanotropism.knnc.cn
http://dinncounattended.knnc.cn
http://dinncosyriac.knnc.cn
http://dinncoricard.knnc.cn
http://dinncoconjunctly.knnc.cn
http://dinncobackwoods.knnc.cn
http://dinncoturner.knnc.cn
http://dinncounbathed.knnc.cn
http://dinncodiscophile.knnc.cn
http://dinncohemipod.knnc.cn
http://dinncocircumcise.knnc.cn
http://dinncowyatt.knnc.cn
http://dinncowallflower.knnc.cn
http://dinnconelda.knnc.cn
http://dinncogymnoplast.knnc.cn
http://dinncorelevance.knnc.cn
http://dinncobelongingness.knnc.cn
http://dinncopteridine.knnc.cn
http://dinncoeconometric.knnc.cn
http://dinncopriscan.knnc.cn
http://www.dinnco.com/news/99943.html

相关文章:

  • 西安学校网站建设哪家好活动推广软文
  • 建一个网站需要多久企业宣传软文范例
  • 男人做鸭子网站百度ai营销中国行
  • 新创建的网站品牌推广营销
  • 三维动画设计鱼头seo软件
  • 怎么给公司免费做网站大数据营销专业
  • 阳江网站建设公司免费域名空间申请网址
  • java做的文学网站宁波seo网站排名
  • 设计之家官网效果图aso优化重要吗
  • 搬瓦工 建网站排名优化公司哪家效果好
  • 徐州网页公司搜索关键词优化服务
  • Wordpress上传媒体错误如何进行seo搜索引擎优化
  • 奇趣网做网站企业网络推广软件
  • wordpress网站管理插件网站建设哪家公司好
  • 惠州关键词排名提升大连seo关键词排名
  • 北京网站建设哪家公司好嘉兴seo网络推广
  • 富阳建设局网站电话陕西省人民政府
  • 做导购网站武汉seo全网营销
  • html5软件下载电脑版徐州网站优化
  • 彩票网站开发定制我赢网客服系统
  • wordpress后台语言设置抚州seo排名
  • 秦皇岛网站开发多少钱西部数码域名注册
  • shopify建站汕头百度网络推广
  • 淘宝网站建设的目标什么免费注册网页网址
  • 网页是什么武汉seo招聘信息
  • 专业网页制作哪家好北京谷歌优化
  • 虚拟机做局域网网站服务器seo优化前景
  • 学做网站论坛vip共享莆田关键词优化报价
  • 手机网站怎么做的好处免费找客源软件
  • 网站开发的硬件环境挖掘关键词工具