1. <tfoot id='Bm3fg'></tfoot>
  2. <small id='Bm3fg'></small><noframes id='Bm3fg'>

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

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

      序列化包含 std::string 的类

      时间:2023-06-04

        <bdo id='7NxJt'></bdo><ul id='7NxJt'></ul>
        <legend id='7NxJt'><style id='7NxJt'><dir id='7NxJt'><q id='7NxJt'></q></dir></style></legend>
      • <small id='7NxJt'></small><noframes id='7NxJt'>

              • <i id='7NxJt'><tr id='7NxJt'><dt id='7NxJt'><q id='7NxJt'><span id='7NxJt'><b id='7NxJt'><form id='7NxJt'><ins id='7NxJt'></ins><ul id='7NxJt'></ul><sub id='7NxJt'></sub></form><legend id='7NxJt'></legend><bdo id='7NxJt'><pre id='7NxJt'><center id='7NxJt'></center></pre></bdo></b><th id='7NxJt'></th></span></q></dt></tr></i><div id='7NxJt'><tfoot id='7NxJt'></tfoot><dl id='7NxJt'><fieldset id='7NxJt'></fieldset></dl></div>
              • <tfoot id='7NxJt'></tfoot>
                  <tbody id='7NxJt'></tbody>
                本文介绍了序列化包含 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 的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文章

                  <bdo id='0Ogi5'></bdo><ul id='0Ogi5'></ul>

                  <small id='0Ogi5'></small><noframes id='0Ogi5'>

                    <legend id='0Ogi5'><style id='0Ogi5'><dir id='0Ogi5'><q id='0Ogi5'></q></dir></style></legend>
                  1. <tfoot id='0Ogi5'></tfoot>
                  2. <i id='0Ogi5'><tr id='0Ogi5'><dt id='0Ogi5'><q id='0Ogi5'><span id='0Ogi5'><b id='0Ogi5'><form id='0Ogi5'><ins id='0Ogi5'></ins><ul id='0Ogi5'></ul><sub id='0Ogi5'></sub></form><legend id='0Ogi5'></legend><bdo id='0Ogi5'><pre id='0Ogi5'><center id='0Ogi5'></center></pre></bdo></b><th id='0Ogi5'></th></span></q></dt></tr></i><div id='0Ogi5'><tfoot id='0Ogi5'></tfoot><dl id='0Ogi5'><fieldset id='0Ogi5'></fieldset></dl></div>