Certainly! Here's a possible implementation of a generic Map in C++ using a hash table:
```cpp
#include <iostream>
#include <unordered_map>
template<typename KeyType, typename ValueType>
class Map {
private:
std::unordered_map<KeyType, ValueType> data;
public:
void insert(const KeyType& key, const ValueType& value) {
data[key] = value;
}
ValueType lookup(const KeyType& key) {
if (data.find(key) != data.end()) {
return data[key];
}
else {
// Handle the case when the key is not found
// For example, you can throw an exception or return a default value
// Here, we are returning a default-constructed ValueType
return ValueType();
}
}
};
int main() {
// Example usage
Map<std::string, int> myMap;
myMap.insert("apple", 10);
myMap.insert("banana", 5);
std::cout << myMap.lookup("apple") << std::endl; // Output: 10
std::cout << myMap.lookup("banana") << std::endl; // Output: 5
std::cout << myMap.lookup("orange") << std::endl; // Output: 0 (default-constructed int)
return 0;
}
```
In this implementation, the `Map` class uses an `unordered_map` from the C++ Standard Library to store the key-value pairs. The `insert` function inserts a key-value pair into the map, and the `lookup` function retrieves the value associated with a given key. If the key is not found, it returns a default-constructed value (you can customize this behavior based on your requirements).