How to reuse hashing function of string class for custom class in unordered map?
-
26-05-2021 - |
Вопрос
I'm defining an unordered_map in C++ like the following:
unordered_map<CustomClass, int, CustomClassHash, CustomClassEq> myMap;
Assume I have been able to successfully define CustomClassEq. I want the hash of CustomClass viz. CustomClassHash to delegate to the hash of a string attribute inside the class. Is there a way that I could reuse the hashing function of string class inside the definition of CustomClassHash?
This is what I want to do:
struct CustomClassHash {
long operator()(const CustomClass &c) const {
string s = c.getString();
// TODO: return the hash of s
}
};
Решение
You can use :
return hash<string>()(c.get_name());
Check the second comment of the post
Не связан с StackOverflow