如何在boost::序列化中遍历归档

How to iterate over archive in boost::serialization

本文关键字:遍历 序列化 boost      更新时间:2023-10-16

我加载了多个数据到boost::archive::text_oarchive,现在我需要提取这些数据。但是因为存档包含多条记录,所以我需要一个迭代器。

之类的
//input archive
boost::archive::text_iarchive iarch(ifs);
//read until the end of file
while (!iarch.eof()){
//read current value
iarch >> temp;
...//do something with temp
}

是否有任何标准的方法来遍历存档的元素?我只找到了iarchive.iterator_type,但它是我需要的吗?我怎么使用它?

您要查看的迭代器类型实际上来自

class shared_ptr_helper {
    ...
    typedef std::set<
        boost::shared_ptr<const void>,
        collection_type_compare
    > collection_type;
    typedef collection_type::const_iterator iterator_type;

,我认为它是在存档加载期间使用的,而不是外部使用的迭代器。

如果您查看tutorial -> STL Collection下的链接http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html,您将看到以下示例:

#include <boost/serialization/list.hpp>
class bus_route
{
    friend class boost::serialization::access;
    std::list<bus_stop *> stops;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & stops;
    }
public:
    bus_route(){}
};

如果这不是你所需要的,那么你可能需要根据http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/tutorial.html#splitting查看重载加载和保存并根据需要添加处理