Boost序列化多个对象

发布于 2024-09-26 03:13:06 字数 129 浏览 5 评论 0原文

我正在尝试构建一个持久性模块,并且正在考虑序列化/反序列化我需要对文件进行持久化的类。 Boost 序列化是否可以将多个对象写入同一个文件?如何读取或循环文件中的条目?如果良好的性能是一个条件,那么 Google 协议缓冲区对我来说可能会更好?

Im trying to build a persistence module and Im thinking in serialize/deserialize the class that I need to make persistence to a file. Is possible with Boost serialization to write multiple objects into the same file? how can I read or loop through entrys in the file? Google protocol buffers could be better for me if a good performance is a condition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-10-03 03:13:06

如果序列化库无法序列化多个对象,那么它就不会很有用。如果您阅读他们的 非常广泛的文档,您可以找到所有答案

A Serialization library wouldn't be very useful if it couldn't serialize multiple objects. You can find all the answers if you read their very extensive documentation.

三人与歌 2024-10-03 03:13:06

我正在学习 boost,我认为您可以使用 boost 序列化作为日志文件,并使用您的逻辑不断添加值。我遇到了同样的问题,如果我没记错的话,您的代码是这样的:

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

int main()  {
    int two=2;

    for(int i=0;i<10;i++)   {

        std::ofstream ofs("table.txt");
        boost::archive::text_oarchive om(ofs);
        om << two;
        two = two+30;
        std::cout<<"\n"<<two;
    }

    return 0;
}

在这里,当您关闭大括号(循环的大括号)时,序列化文件将关闭。你可能会看到 table.txt 中只写了一个值,如果你想存储多个值,你的代码应该是这样的:

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

int main()  {
    int two=2;

    {
        std::ofstream ofs("table.txt");
        boost::archive::text_oarchive om(ofs);
        for(int i=0;i<10;i++)   {

            om << two;
            two = two+30;
            std::cout<<"\n"<<two;
        }
    }

    return 0;
}

在这里你可以看到包围 boost::serialization::text_oarchive 的大括号仅在我完成了我的逻辑结果的序列化。

I am learning boost, and I think you can use boost serialization as a log file and keep adding values using your logic. I faced the same problem, and if I'm not wrong your code was something like this :

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

int main()  {
    int two=2;

    for(int i=0;i<10;i++)   {

        std::ofstream ofs("table.txt");
        boost::archive::text_oarchive om(ofs);
        om << two;
        two = two+30;
        std::cout<<"\n"<<two;
    }

    return 0;
}

Here when you close the braces (braces of the loop), the serialization file closes. And you may see only one value written in table.txt , if you want to store multiple values, your code should be something like this:

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

int main()  {
    int two=2;

    {
        std::ofstream ofs("table.txt");
        boost::archive::text_oarchive om(ofs);
        for(int i=0;i<10;i++)   {

            om << two;
            two = two+30;
            std::cout<<"\n"<<two;
        }
    }

    return 0;
}

Here you can see that the braces enclosing boost::serialization::text_oarchive closes only when I'm done with serialization of the result of my logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文