Abhinav Aggarwal
Abhinav Aggarwal

Reputation: 11

C++ map comparator

For a c++ map declared like:

map < set<int>,int > x;

What is the default comparator function that the compiler uses?
My code using this statement executed successfully but I am not sure about the comparator it uses.

Upvotes: 1

Views: 241

Answers (3)

Russell Greene
Russell Greene

Reputation: 2261

For both std::map and std::set there is a optional template parameter that defines the comparison type (and another one for the allocator.) using this you can use any comparison, including a user defined function!

In both these cases, it defaults to std::less

Look at the docs for each!!

http://www.cplusplus.com/reference/set/set/

http://www.cplusplus.com/reference/map/map/

Upvotes: 0

R Sahu
R Sahu

Reputation: 206577

For a std::map<K, T>, the default comparator is std::less<K>.

std::less<K> uses lhs < rhs as the default way to compare.

In your case, you end up using:

bool operator<(std::set<int> const& lhs, std::set<int> const& rhs)

Upvotes: 3

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

For default this should resolve to

bool operator<(const std::set<int>&, const std::set<int>&)

See here for reference.

Upvotes: 2

Related Questions