سؤال

I know that you cannot change the ordering of a map once declared. Instead I am trying this in a struct:

struct MyStruct
{
    std::map<int, double>* my_map;

    MyStruct(bool dir)
    {
        if(dir)
        {
            my_map = new std::map<int, double, std::less<int> >;
        }
        else
        {
            my_map = new std::map<int, double, std::greater<int> >;
        }
    }
}

This isn't working and complains that I am changing the type under the else condition. Is there a way around this? The only way I can think of is to write my own comparator and to create an object encapsulating bool dir which seems redundant.

هل كانت مفيدة؟

المحلول

std::map takes the comparison object as a template parameter, so to do what you want you need a type that you can change the behaviour at runtime.

    struct MoreOrLess
    {
      bool useLess;
      template <class T, class U>
      bool operator()(const T &t, const U &u) const
      {
        if(useLess) return t < u;
        else return t > u;
      }
    };

    struct MyStruct
    {
        std::map<int, double, MoreOrLess> my_map;

        MyStruct(bool dir) :my_map(MoreOrLess{dir}) {}
    };

This way the comparison functor has the same type (for use in std::map) regardless of using std::less or std::greater.

نصائح أخرى

Here is a simple approach, although probably not the most efficient:

struct MyStruct
{
    typedef std::function<bool(int,int)> Predicate;
    std::map<int,double,Predicate> my_map;

    static Predicate predicateFor(bool dir)
    {
        if (dir) return std::less<int>();
        return std::greater<int>();
    }

    MyStruct(bool dir) : my_map(predicateFor(dir)) { }
};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top