How to Implement Hash Map in C++: Syntax and Example
In C++, you can implement a hash map using the
std::unordered_map container from the standard library, which stores key-value pairs with fast access. It uses hashing internally to quickly find values by their keys.Syntax
The basic syntax to declare a hash map in C++ is using std::unordered_map<KeyType, ValueType>. You include the header <unordered_map> and create an object. Keys must be unique, and values can be accessed or inserted using the square bracket [] operator.
- KeyType: Type of the keys (e.g.,
int,std::string). - ValueType: Type of the values stored.
- insert(): Method to add key-value pairs.
- [] operator: Access or insert values by key.
cpp
#include <unordered_map> #include <string> std::unordered_map<KeyType, ValueType> map; // Insert or update map[key] = value; // Access ValueType val = map[key];
Example
This example shows how to create a hash map that stores names as keys and ages as values. It inserts some entries, updates one, and prints all pairs.
cpp
#include <iostream> #include <unordered_map> #include <string> int main() { std::unordered_map<std::string, int> ageMap; // Insert key-value pairs ageMap["Alice"] = 30; ageMap["Bob"] = 25; ageMap["Charlie"] = 35; // Update value ageMap["Bob"] = 26; // Iterate and print for (const auto& pair : ageMap) { std::cout << pair.first << ": " << pair.second << "\n"; } return 0; }
Output
Alice: 30
Bob: 26
Charlie: 35
Common Pitfalls
Common mistakes when using std::unordered_map include:
- Using mutable keys or keys without a proper hash function (for custom types).
- Accessing a key with
[]that does not exist inserts a default value, which may be unexpected. - Not including the
<unordered_map>header. - Assuming order of elements;
unordered_mapdoes not keep insertion order.
cpp
#include <unordered_map>
#include <string>
struct Point {
int x, y;
};
// Wrong: Using custom type without hash
// std::unordered_map<Point, int> map; // Error: no hash function for Point
// Right: Provide a hash function and equality operator
#include <functional>
struct PointHash {
std::size_t operator()(const Point& p) const {
return std::hash<int>()(p.x) ^ (std::hash<int>()(p.y) << 1);
}
};
struct PointEqual {
bool operator()(const Point& a, const Point& b) const {
return a.x == b.x && a.y == b.y;
}
};
std::unordered_map<Point, int, PointHash, PointEqual> map;
Quick Reference
| Feature | Description |
|---|---|
| Include header | #include <unordered_map> |
| Declare map | std::unordered_map<Key, Value> map; |
| Insert/update | map[key] = value; |
| Access | Value val = map[key]; (inserts default if key missing) |
| Check existence | map.find(key) != map.end() |
| Erase key | map.erase(key); |
| Iterate | for (auto &pair : map) { ... } |
Key Takeaways
Use
std::unordered_map for fast key-value storage in C++.Always include
<unordered_map> and use correct key and value types.Accessing a missing key with
[] inserts a default value; use find() to check existence.For custom key types, provide a hash function and equality operator.
Elements in
unordered_map have no guaranteed order.