bit-question
bit-question

Reputation: 3843

the type of map's key

Just saw a question on STL. The question is "<"does not need to be overloaded when the key of map belongs to certain types. What are these types?

Don't quite understand this question! Thanks for answering.

Upvotes: 1

Views: 149

Answers (2)

Puppy
Puppy

Reputation: 146910

Basically, primitive types for which < is already defined.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254451

Assuming the map is instantiated with the default comparator (i.e. as map<Key,Value> with no third argument):

  • Built-in numeric types
  • Pointers
  • Any type for which std::less<Key> has been specialised (as long as the specialisation doesn't require operator<).
  • Pedantically, any type which already has an overload of operator<.

For any other key type, the map will try to compare them using an expression like key1 < key2, which will only compile if there is an overload of operator< for the key type.

Upvotes: 2

Related Questions