开发者

the type of map's key

开发者 https://www.devze.com 2023-02-25 15:22 出处:网络
Just saw a question on STL.The question is \"<\"does not need to be overloaded whenthe key of map belongs to certain types.

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 thes开发者_JAVA百科e types?

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


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.


Basically, primitive types for which < is already defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消