Hash Sets using std::unordered_set

Implementing a Custom Hash Function

How can I implement a custom hash function for my user-defined type to use with std::unordered_set?

Abstract art representing computer programming

To implement a custom hash function for your user-defined type, you need to create a struct that overloads the operator(). Here's an example:

#include <unordered_set>
#include <string>

struct Player {
  std::string Name;
  int Score;
};

struct PlayerHash {
  size_t operator()(const Player& P) const {
    return std::hash<std::string>{}(P.Name)
         ^ std::hash<int>{}(P.Score);
  }
};

For our type to be compatible with hash-based containers such as std::unordered_set, it additionally needs to implement the == operator:

#include <string>
#include <unordered_set>

struct Player {
  std::string Name;
  int Score;

  bool operator==(const Player& Other) const {
    return Name == Other.Name;
  }
};

struct PlayerHash {
  size_t operator()(const Player& P) const {
    return std::hash<std::string>{}(P.Name)
       ^ std::hash<int>{}(P.Score); 
  }
};

int main() {
  std::unordered_set<Player, PlayerHash> Players;
  Players.emplace("Alice", 100);
  Players.emplace("Bob", 200);
}

In this example:

  • We define a¬†Player¬†struct with¬†Name¬†and¬†Score¬†members, and operator==() to allow Player objects to be compared for equality. In this case, we consider two Player objects to be equal if they have the same Name.
  • We create a¬†PlayerHash¬†struct that overloads¬†operator()
  • Inside¬†operator(), we combine the hashes of¬†Name¬†and¬†Score¬†using XOR (^). This ensures that players with the same name but different scores (or vice versa) will have different hashes.
  • We specify¬†PlayerHash¬†as the second template argument when creating the¬†std::unordered_set

By providing a custom hash function, we enable std::unordered_set to efficiently store and retrieve Player objects.

Answers to questions are automatically generated and may not have been reviewed.

Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved