How do I create a hash function for key as pair of string and enum to be used in unordered_map?
I have the following pair which is the key to an unordered map how do I create a hash so it can be used in unordered_map?
enum Color {
Red,
Green
};
typedef std::pair <std::string, Color>
I tried the following but got compilation error
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
size_t hash( const PairType & pair ) {
return std::hash<std::string>()(pair.first) ^
std::hash<EnumClassHash>()(pair.second);
}
typedef std::unordered_map<PairType, std::string>,
std::function<decltype(hash)>> ColorMapType;
error: invalid use of incomplete type 'struct std::hash<EnumClassHash>'
std::hash<EnumClassHash>()(pairType.second);
The following isnt working either
size_t hash( const PairType & pairType ) {
return std::hash<std::string>()(pairType.first) ^
std::hash<Color>()(pairType.second);
}
typedef std::unordered_map<PairType, std::string, \
std::function<decltype(hash)>> ColorMapType;
ColorMapType colorMap(100, hash);
error: invalid use of incomplete type 'struct std::hash<Color>'
std::hash<Color>()(pair.second
);
PS Pardon my formatting. I lost my gmail password and can only post from mobile app which is a tiny screen.