The operator <overload for comparison std :: map int type? (I want it to sort in descending order.)

I have a problem with the fact that I want to identify a card that is sorted internally in the first descending order. if the first is not the main type, for example, if it is a class, I can simply overload the "<" inside this class, but I do not know how to deal with the int type. Any suggestion?

Thanks a lot!

+4
source share
3 answers

Add comparator:

 #include <functional> map<int, value_type, greater<int>> m; 

The default is less<int> .

+11
source

You can specify a comparator when creating the map (this is an optional constructor argument).

eg:.

 bool my_compare(int x, int y) { return y < x; } std::map<int,T,bool(*)(int,int)> my_map(my_compare); 

Please note that I need to explicitly specify the 3rd template parameter.

[Note: I would strongly advised you not to overload the operator < for execution > ...]

+1
source

Look at the implementation of std :: less: http://www.cplusplus.com/reference/std/functional/less/ You can write your own comparator and use it with the map.

0
source

Source: https://habr.com/ru/post/1388352/


All Articles