如何对 std::stack 使用 boost::serialization?

发布于 2025-01-02 10:09:15 字数 222 浏览 0 评论 0原文

我知道我们可以将 boost::serialization 用于 std::vector 、 list 、map 等,只要我包含相应的 boost 头文件,例如:

#include <boost/serialization/list.hpp>

但是如何将 boost::serialization 用于 std::stack ?

谢谢

I know we can use boost::serialization for std::vector , list ,map etc as long as I include the corresponding boost header file like:

#include <boost/serialization/list.hpp>

But how could I use boost::serialization for std::stack ?

Thanks

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

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

发布评论

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

评论(2

岛徒 2025-01-09 10:09:15

我知道这已经很旧了,但我最近遇到了这个问题。我对使用 std::deque 的解决方案并不满意,因为我实际上只需要一个堆栈接口。目前,boost 通过按此顺序包含这些头文件来支持 std::stack 的序列化。

#include "boost/serialization/deque.hpp"
#include "boost/serialization/stack.hpp"

完整的示例如下所示:

#include <string>
#include <stack>
#include "boost/archive/text_oarchive.hpp"
#include "boost/archive/text_iarchive.hpp"
#include "boost/serialization/deque.hpp"
#include "boost/serialization/stack.hpp"

struct s
{
    std::stack<std::string> stack;

private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & stack;
    }
};

I know this is old, but I recently came across this problem. I was not satisfied with the solution of using std::deque because I really needed only a stack interface. Currently, boost support serialization of std::stack this by including these header files in this order

#include "boost/serialization/deque.hpp"
#include "boost/serialization/stack.hpp"

A complete example would look something like:

#include <string>
#include <stack>
#include "boost/archive/text_oarchive.hpp"
#include "boost/archive/text_iarchive.hpp"
#include "boost/serialization/deque.hpp"
#include "boost/serialization/stack.hpp"

struct s
{
    std::stack<std::string> stack;

private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & stack;
    }
};
故事未完 2025-01-09 10:09:15

std::stack 替换为 std::deque 并包含:

#include <boost/serialization/deque.hpp>

Replace std::stack with std::deque and include:

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