Link to home
Create AccountLog in
Avatar of Rooh
RoohFlag for India

asked on

Preventing sorting in std::map

Hi,
    How can I prevent sorting in c++ std::map?
Thanks,
Avatar of mccarl
mccarl
Flag of Australia image

The std::map always orders the entries in some order, which you can specify/change by supplying its comparison object.

However, is std::unordered_map what you are after then?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
If you are putting your own class or struct in the map, you can overload the < operator to always return false. Or you can pass a comparator in the constructor that always returns false. This will make the map think all the elements are equal and it will not sort. Of course, if you prevent sorting, map lookups will be much less efficient. If you want efficiency without sorting, try some kind of hash table.