有人可以帮助我了解如何使用比较器序列化映射。保存后,map知道比较器的类,但是map不知道比较器的字段。

bool operator() (const ScalarT &a, const ScalarT &b) const{
    return (a - someField < b);
}

最佳答案

啊。很好的问题。这是boost中序列化的限制。

在有状态比较器的情况下,它不会序列化比较器状态。

在SSCCE中演示该问题:Live On Coliru,它会打印

====== original order
3 -> three
2 -> two
1 -> one
====== deserialized order
1 -> one
2 -> two
3 -> three


这是有道理的,因为否则将需要使所有比较器可序列化。这就带来了一些棘手的问题(如果Cmp为std::function<bool(K)>会怎样?如果实际值为lambda会怎样?)。



因此,长话短说:硬着头皮,也只是序列化您的比较器:

Live On Coliru

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <iostream>
#include <sstream>

template <typename T>
struct StateFullCmp {
    bool invert_;
    StateFullCmp(bool invert) : invert_(invert) {}

    bool operator()(T const&a, T const& b) const {
        return invert_? (b<a) : (a<b);
    }
};

namespace boost { namespace serialization {
    template <typename Ar, typename T>
        void serialize(Ar& ar, StateFullCmp<T>& cmp, unsigned) {
            ar & cmp.invert_;
        }
} }

int main() {
    using MyMap = std::map<int, std::string, StateFullCmp<int> >;
    std::stringstream ss;

    {
        MyMap const m {
            { { 1, "one" }, { 2, "two" }, { 3, "three" } },
            StateFullCmp<int> { true } // inverted order
        };

        std::cout << "====== original order\n";
        for (auto& e : m)
            std::cout << e.first << " -> " << e.second << "\n";

        boost::archive::text_oarchive oa(ss);
        auto cmp_copy = m.key_comp();
        oa << cmp_copy << m;
    }

    {
        boost::archive::text_iarchive ia(ss);

        MyMap::key_compare cmp_state { false }; // default initialization
        ia >> cmp_state;

        MyMap deserialized(cmp_state);
        ia >> deserialized;

        std::cout << "====== deserialized order\n";
        for (auto& e : deserialized)
            std::cout << e.first << " -> " << e.second << "\n";
    }
}


现在打印:

====== original order
3 -> three
2 -> two
1 -> one
====== deserialized order
3 -> three
2 -> two
1 -> one

关于c++ - 使用比较器 boost 序列化映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31561989/

10-11 18:06