Question

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
    }
};
Was it helpful?

Solution

You can use : return hash<string>()(c.get_name());

Check the second comment of the post

object as a key of unordered map

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top