• <bdo id='pQbG7'></bdo><ul id='pQbG7'></ul>

      <i id='pQbG7'><tr id='pQbG7'><dt id='pQbG7'><q id='pQbG7'><span id='pQbG7'><b id='pQbG7'><form id='pQbG7'><ins id='pQbG7'></ins><ul id='pQbG7'></ul><sub id='pQbG7'></sub></form><legend id='pQbG7'></legend><bdo id='pQbG7'><pre id='pQbG7'><center id='pQbG7'></center></pre></bdo></b><th id='pQbG7'></th></span></q></dt></tr></i><div id='pQbG7'><tfoot id='pQbG7'></tfoot><dl id='pQbG7'><fieldset id='pQbG7'></fieldset></dl></div>

      1. <tfoot id='pQbG7'></tfoot>

      2. <small id='pQbG7'></small><noframes id='pQbG7'>

        <legend id='pQbG7'><style id='pQbG7'><dir id='pQbG7'><q id='pQbG7'></q></dir></style></legend>
      3. 序列化包含 std::string 的类

        时间:2023-10-06

          <tbody id='QUboL'></tbody>
        <tfoot id='QUboL'></tfoot>

      4. <small id='QUboL'></small><noframes id='QUboL'>

          1. <i id='QUboL'><tr id='QUboL'><dt id='QUboL'><q id='QUboL'><span id='QUboL'><b id='QUboL'><form id='QUboL'><ins id='QUboL'></ins><ul id='QUboL'></ul><sub id='QUboL'></sub></form><legend id='QUboL'></legend><bdo id='QUboL'><pre id='QUboL'><center id='QUboL'></center></pre></bdo></b><th id='QUboL'></th></span></q></dt></tr></i><div id='QUboL'><tfoot id='QUboL'></tfoot><dl id='QUboL'><fieldset id='QUboL'></fieldset></dl></div>
              <bdo id='QUboL'></bdo><ul id='QUboL'></ul>
                  <legend id='QUboL'><style id='QUboL'><dir id='QUboL'><q id='QUboL'></q></dir></style></legend>
                • 本文介绍了序列化包含 std::string 的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我不是 C++ 专家,但过去我已经将事情序列化了几次.不幸的是,这次我试图序列化一个包含 std::string 的类,我理解这很像序列化一个指针.

                  I'm not a c++ expert but I've serialized things a couple of times in the past. Unfortunately this time I'm trying to serialize a class which contains a std::string, which I understand is pretty much like serializing a pointer.

                  我可以将类写出到文件中,然后再读回.所有 int 字段都很好,但 std::string 字段给出了地址越界"错误,大概是因为它指向不再存在的数据.

                  I can write out the class to a file and read it back in again. All int fields are fine but the std::string field gives an "address out of bounds" error, presumably because it points to data which is no longer there.

                  对此有标准的解决方法吗?我不想回到字符数组,但至少我知道它们在这种情况下工作.如有必要,我可以提供代码,但我希望我已经很好地解释了我的问题.

                  Is there a standard workaround for this? I don't want to go back to char arrays but at least I know they work in this situation. I can provide code if necessary but I'm hoping I've explained my problem well.

                  我通过将类转换为 char* 并使用 fstream 将其写入文件来进行序列化.阅读当然正好相反.

                  I'm serializing by casting the class to a char* and writing it to a file with fstream. Reading of course is just the reverse.

                  推荐答案

                  我通过将类转换为 char* 并将其写入文件与 fstream.阅读当然正好相反.

                  I'm serializing by casting the class to a char* and writing it to a file with fstream. Reading of course is just the reverse.

                  不幸的是,这仅在不涉及指针时才有效.你可能想给你的类 void MyClass::serialize(std::ostream)void MyClass::deserialize(std::ifstream),然后调用它们.对于这种情况,您需要

                  Unfortunately, this only works as long as there are no pointers involved. You might want to give your classes void MyClass::serialize(std::ostream) and void MyClass::deserialize(std::ifstream), and call those. For this case, you'd want

                  std::ostream& MyClass::serialize(std::ostream &out) const {
                      out << height;
                      out << ',' //number seperator
                      out << width;
                      out << ',' //number seperator
                      out << name.size(); //serialize size of string
                      out << ',' //number seperator
                      out << name; //serialize characters of string
                      return out;
                  }
                  std::istream& MyClass::deserialize(std::istream &in) {
                      if (in) {
                          int len=0;
                          char comma;
                          in >> height;
                          in >> comma; //read in the seperator
                          in >> width;
                          in >> comma; //read in the seperator
                          in >> len;  //deserialize size of string
                          in >> comma; //read in the seperator
                          if (in && len) {
                              std::vector<char> tmp(len);
                              in.read(tmp.data() , len); //deserialize characters of string
                              name.assign(tmp.data(), len);
                          }
                      }
                      return in;
                  }
                  

                  您可能还想重载流运算符以便于使用.

                  You may also want to overload the stream operators for easier use.

                  std::ostream &operator<<(std::ostream& out, const MyClass &obj)
                  {obj.serialize(out); return out;}
                  std::istream &operator>>(std::istream& in, MyClass &obj)
                  {obj.deserialize(in); return in;}
                  

                  这篇关于序列化包含 std::string 的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:读取由空格或换行符分隔的输入...? 下一篇:C++中如何实现序列化

                  相关文章

                  最新文章

                  <small id='yG8zZ'></small><noframes id='yG8zZ'>

                      • <bdo id='yG8zZ'></bdo><ul id='yG8zZ'></ul>
                      <i id='yG8zZ'><tr id='yG8zZ'><dt id='yG8zZ'><q id='yG8zZ'><span id='yG8zZ'><b id='yG8zZ'><form id='yG8zZ'><ins id='yG8zZ'></ins><ul id='yG8zZ'></ul><sub id='yG8zZ'></sub></form><legend id='yG8zZ'></legend><bdo id='yG8zZ'><pre id='yG8zZ'><center id='yG8zZ'></center></pre></bdo></b><th id='yG8zZ'></th></span></q></dt></tr></i><div id='yG8zZ'><tfoot id='yG8zZ'></tfoot><dl id='yG8zZ'><fieldset id='yG8zZ'></fieldset></dl></div>
                      <tfoot id='yG8zZ'></tfoot>

                      <legend id='yG8zZ'><style id='yG8zZ'><dir id='yG8zZ'><q id='yG8zZ'></q></dir></style></legend>