How do I make my custom type work with std::unordered_map?

I have a custom type that I want to use as a key in std::unordered_map. What do I need to do to make it work?

To use a custom type as a key in std::unordered_map, you need to specialize the std::hash template for your type. The std::hash template is used by std::unordered_map to compute the hash value of the keys.

Here's how you can specialize std::hash for your custom type:

  1. Create a specialization of std::hash for your type.
  2. Implement the operator() function that takes an object of your type and returns a hash value.

Here's an example:

#include <string>
#include <unordered_map>

struct MyType {
  int id;
  std::string name;
  // ... other members ...
};

namespace std {
template <>
struct hash<MyType> {
  size_t operator()(const MyType& obj) const {
    size_t idHash = std::hash<int>()(obj.id);
    size_t nameHash = std::hash<
      std::string>()(obj.name);
    // Combine the hash values
    return idHash ^ (nameHash << 1);
  }
};
}

In this example, MyType is a custom type with an id and a name member. The specialization of std::hash<MyType> is defined in the std namespace.

The operator() function takes an object of MyType and computes its hash value. In this case, we compute the hash values of the id and name members separately using std::hash<int> and std::hash<std::string>, respectively. Then, we combine the hash values using the XOR (^) operator and a bit shift (<<) to create the final hash value.

Once you have specialized std::hash for your type, you can use MyType as a key in std::unordered_map:

std::unordered_map<MyType, int> myMap;
MyType obj1{1, "Object 1"};
MyType obj2{2, "Object 2"};

myMap[obj1] = 10;
myMap[obj2] = 20;

The std::unordered_map will use the specialized std::hash<MyType> to compute the hash values of the keys and efficiently store and retrieve the elements.

Note: Make sure that your hash function provides a good distribution of hash values to minimize collisions and ensure efficient performance of the std::unordered_map.

Template Specialization

A practical guide to template specialization in C++ covering full and partial specialization, and the scenarios where they're useful

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

When should I use template specialization in C++?
In what scenarios is it beneficial to use template specialization in C++? Can you provide some practical examples?
What is the difference between full and partial template specialization?
Can you explain the difference between full and partial template specialization in C++? When would you use each one?
How do I specialize a member function of a class template?
I have a class template with a member function. How can I specialize that member function for a specific type?
What is the difference between template specialization and function overloading?
How does template specialization differ from function overloading in C++? When should I use each one?
Ask Your Own Question
Temporarily unavailable while we roll out updates. Back in a few days!