使用比较器

时间:2015-07-22 11:36:32

标签: c++ dictionary serialization boost comparator

有人可以帮助我理解如何用比较器序列化地图。保存地图后知道比较器类,但地图不知道比较器字段。

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

1 个答案:

答案 0 :(得分:0)

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

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

在SSCCE中展示问题: Live On Coliru ,打印

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

这是有道理的,因为否则会要求所有比较器都可以序列化。这会带来 hard 问题(如果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