Using std::unordered_set with Smart Pointers
How can I store smart pointers like std::unique_ptr in a std::unordered_set?
To store smart pointers like std::unique_ptr in a std::unordered_set, you need to provide a custom hash function and a custom equality comparator that operate on the underlying raw pointers.
Here's an example of using std::unique_ptr with std::unordered_set:
#include <iostream>
#include <memory>
#include <unordered_set>
struct Player {
std::string Name;
int Score;
Player(const std::string& Name, int Score)
: Name{Name}, Score{Score} {}
};
struct PlayerHash {
size_t operator()(
const std::unique_ptr<Player>& P) const {
return std::hash<Player*>{}(P.get());
}
};
struct PlayerEqual {
bool operator()(
const std::unique_ptr<Player>& A,
const std::unique_ptr<Player>& B
) const {
return A.get() == B.get();
}
};
int main() {
std::unordered_set<std::unique_ptr<Player>,
PlayerHash, PlayerEqual> Players;
Players.emplace(
std::make_unique<Player>("Alice", 100));
Players.emplace(
std::make_unique<Player>("Bob", 200));
for (const auto& Player : Players) {
std::cout << Player->Name << ": "
<< Player->Score << "\n";
}
}Alice: 100
Bob: 200In this example:
- We define a
Playerstruct withNameandScoremembers - We create a
PlayerHashstruct that overloadsoperator()to hash the raw pointer stored inside thestd::unique_ptr - We create a
PlayerEqualstruct that overloadsoperator()to compare the raw pointers stored inside thestd::unique_ptrs for equality - We specify
std::unique_ptr<Player>as the key type and providePlayerHashandPlayerEqualas the hash function and equality comparator when creating thestd::unordered_set - We use
std::make_uniqueto createstd::unique_ptr<Player>objects and insert them into the set usingemplace()
By providing custom hash and equality functions that operate on the raw pointers, we enable std::unordered_set to store and compare std::unique_ptr<Player> objects correctly.
Note that using smart pointers in an unordered set compares the pointers themselves, not the objects they point to. If you want to compare the pointed-to objects, you'll need to dereference the smart pointers in your custom comparator and hash function.
Hash Sets using std::unordered_set
This lesson provides a thorough understanding of std::unordered_set, from basic initialization to handling custom types and collisions