Search code examples
c++dictionarydata-structuresstdkeyvaluepair

How std::map maps a pair as a key to its value without a comparison function


If we use a structure or a class as a key then a comparison function is required to place the values in the tree but if a pair is used as a key then how the map data structure place the values in the tree. I.e. there has to be something to compare the keys and store them in to a tree.


Solution

  • The default comparison function for std::map is std::less using the Key type for the arguments. std::less just calls the < operator on its arguments, which is defined for std::pair (it compares first and second lexicographically, using their operator<).