0

if I have a field in class like below,

class MyClass {
public:
    shared_ptr<map<int, int>> my_map;
}

The default map<int,int>'s comparison function is less<int>, but I would like to initialize my_map as

my_map = make_shared<map<int, int, greater<int>>>();

sometimes,

This doesnt compile, what other way I can have a shared_ptr<map<int, int>> that in two different object of MyClass, one has less<int> and the other have greater<int> so that the my_map in each object is sorted differently?

4
  • You should include the error message from your compiler, verbatim. That would point our your typo.
    – Eljay
    Aug 22, 2020 at 14:37
  • 1
    What error do you get, maybe you just need to separate the two > brackets (if you are compiling against a standard < c++17).
    – Mansoor
    Aug 22, 2020 at 14:37
  • Can you stick your whole code in the question? Makes it easier to see what might be wrong.
    – Carlos
    Aug 22, 2020 at 14:40
  • May I ask why do you need to shared_ptr<> your map? Aug 22, 2020 at 15:00

4 Answers 4

2

what other way I can have a shared_ptr<map<int, int>> that in two different object of MyClass, one has less<int> and the other have greater<int> so that the my_map in each object is sorted differently?

You can specify std::function as the template argument for comparator as

std::shared_ptr<std::map<int, int, std::function<bool(int,int)>>> my_map;

and initialize it with std::less<int>

my_map = std::make_shared<std::map<int, int, std::function<bool(int,int)>>>(std::less<int>{});

or std::greater<int>.

my_map = std::make_shared<std::map<int, int, std::function<bool(int,int)>>>(std::greater<int>{});
4
  • It woukd be expensive to do std::function<bool(int,int)>, where it fits with just uing functionPointer = bool(*)(int)(int).
    – Const
    Aug 22, 2020 at 19:20
  • @Const But std::less and std::greater can't be used with function pointer. Aug 23, 2020 at 1:15
  • thanks for this answer, i have tested, it works, just a further question, I believe you are refering to this constructor explicit map (const key_compare& comp, const allocator_type& alloc = allocator_type()); here cplusplus.com/reference/map/map/map ? How did you know that the return type is std::map<int, int, std::function<bool(int,int)>>? I cant find this anywhere in the official doc.
    – tesla1060
    Aug 24, 2020 at 1:43
  • @tesla1060 Yes, that constructor is used. The type, i.e. std::map<int, int, std::function<bool(int,int)>> is specified as the template argument when calling std::make_shared, then we'll get a shared_ptr pointing to std::map<int, int, std::function<bool(int,int)>>. Aug 24, 2020 at 1:47
2

You can use a custom comparator, something like:

struct my_comparator {

     bool the_other_way=false;

     my_comparator(bool flag) : the_other_way{flag} {}

     bool operator()(int a, int b) const
     {
         return the_other_way ? a > b: a < b;
     }
};

And construct a std::map<int, int, my_comparator>.

std::map has an overloaded constructor that takes an instance of the comparator object as a parameter. You can simply construct each individual map, accordingly.

typedef std::map<int, int, my_comparator> my_map;

my_map m1{my_comparator{false}};

my_map m2{my_comparator{true}};
0
1

If you want to customize the internal map with a different comparator for different objects, you can template the class to accept a comparison object, like this:

template<typename Compare = std::less<int>>
class MyClass 
{
  public:
    std::shared_ptr<std::map<int, int, Compare>> my_map;
};

and then create different objects:

int main()
{
    MyClass<> a;   // std::less<int> by default
    MyClass<std::greater<int>> b;
}

Here's a demo.

0

The comparator is part of the type declaration, so your member would need to include the comparator type:

class MyClass {
public:
    shared_ptr<map<int, int, greater<int>>> my_map;
}

Also, you are missing a '>' in the assignment. It should be:

my_map = make_shared<map<int, int, greater<int>>>();

See cigien's answer if you want to templatize your class to use different comparators (as the question implies).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.