how can i implkement a hash map in c++
- User
how can i implkement a hash map in c++
- Assistant
how can i implkement a hash map in c++
Implementing a hash map in C++ involves creating a data structure that supports efficient insertion, deletion, and lookup operations, typically achieving average-case time complexity of O(1) for these operations. Below is a simple implementation of a hash map using separate chaining to handle collisions, where each bucket is a linked list:
#include <iostream>
#include <vector>
#include <list>
#include <utility> // for std::pair
template <typename KeyType, typename ValueType>
class HashMap {
private:
static const int DEFAULT_BUCKETS = 10;
// Vector of buckets (each bucket is a list of key-value pairs)
std::vector<std::list<std::pair<KeyType, ValueType>>> buckets;
int hashFunction(const KeyType& key) const {
// Simple hash function using std::hash
return std::hash<KeyType>()(key) % buckets.size();
}
public:
HashMap(int numBuckets = DEFAULT_BUCKETS) : buckets(numBuckets) {}
void insert(const KeyType& key, const ValueType& value) {
int index = hashFunction(key);
for (auto& kvp : buckets[index]) {
if (kvp.first == key) {
kvp.second = value; // Update existing key's value
return;
}
}
buckets[index].emplace_back(key, value);
}
bool get(const KeyType& key, ValueType& value) const {
int index = hashFunction(key);
for (const auto& kvp : buckets[index]) {
if (kvp.first == key) {
value = kvp.second;
return true;
}
}
return false; // Key not found
}
void remove(const KeyType& key) {
int index = hashFunction(key);
auto& bucket = buckets[index];
for (auto it = bucket.begin(); it != bucket.end(); ++it) {
if (it->first == key) {
bucket.erase(it);
return;
}
}
}
void printHashMap() const {
for (int i = 0; i < buckets.size(); ++i) {
std::cout << "Bucket " << i << ": ";
for (const auto& kvp : buckets[i]) {
std::cout << "{" << kvp.first << ": " << kvp.second << "} ";
}
std::cout << std::endl;
}
}
};
int main() {
HashMap<std::string, int> myHashMap;
myHashMap.insert("apple", 1);
myHashMap.insert("banana", 2);
myHashMap.insert("orange", 3);
int value;
if (myHashMap.get("banana", value)) {
std::cout << "The value for 'banana' is: " << value << std::endl;
} else {
std::cout << "'banana' not found in the hash map." << std::endl;
}
myHashMap.remove("banana");
if (myHashMap.get("banana", value)) {
std::cout << "The value for 'banana' is: " << value << std::endl;
} else {
std::cout << "'banana' not found in the hash map." << std::endl;
}
myHashMap.printHashMap();
return 0;
}
Hash Function: The hashFunction uses std::hash to get a hash value for the key, then maps it to a bucket index using modulo operator with the number of buckets.
Insert Operation: The insert function looks for the key in the appropriate bucket and updates its value if found; otherwise, it inserts a new key-value pair into the bucket.
Get Operation: The get function retrieves the value associated with the given key if present.
Remove Operation: The remove function deletes the key-value pair from the bucket if the key is found.
Collision Handling: Separate chaining is used to handle collisions, where each bucket is a linked list of elements.
This basic implementation can be further optimized with dynamic resizing, better hash functions, or different collision resolution strategies.
Not yet included in a published release.