#include <iostream>
#include <unordered_map>
#include <string>
// Custom hash function for a custom data structure
struct CustomData {
int intValue;
std::string stringValue;
bool operator==(const CustomData& other) const {
return intValue == other.intValue && stringValue == other.stringValue;
}
};
// Custom hash function for the CustomData structure
struct CustomDataHash {
std::size_t operator()(const CustomData& data) const {
// Combine hash values of individual members using bitwise XOR
std::size_t hashValue = 0;
hashValue ^= std::hash<int>()(data.intValue);
hashValue ^= std::hash<std::string>()(data.stringValue);
return hashValue;
}
};
int main() {
// Create an unordered_map using CustomData as key and int as value
std::unordered_map<CustomData, int, CustomDataHash> customMap;
// Example data
CustomData data1{42, "Hello"};
CustomData data2{77, "World"};
// Insert data into the map
customMap[data1] = 1;
customMap[data2] = 2;
// Access values using custom keys
std::cout << "Value for data1: " << customMap[data1] << std::endl;
std::cout << "Value for data2: " << customMap[data2] << std::endl;
return 0;
}