I'm trying to get customized hash table working on custom types. Referring to unordered_map constructor error (equal_to templated function)
I have:
typedef pair<int, int> tCoord; struct hashing_func { unsigned long operator()(const tCoord& key) const { unsigned long hash = 0; int h1 = key.first; int h2 = key.second; return h1 ^ (h2 << 1); } }; struct key_equal_fn { bool operator()(const tCoord& t1, const tCoord& t2) const { return t1.first == t2.first && t1.second == t2.second; } }; unordered_map<tCoord, int, hashing_func, key_equal_fn> coord2cnt; unordered_map<tCoord, int, hashing_func, key_equal_fn>::iterator iter; iter = coord2cnt.find(coord); This snippet didn't compile and complained about missing function call to find():
error: no matching member function for call to 'find' Also tried to use it as coord2cnt[coord], but also got errors on missing [] operator.
I'm compiling using g++ 4.2, which is a bit old, but was fine compiling the following (from the above link):
typedef unordered_map<string, string, hashing_func, key_equal_fn> MapType; MapType::size_type n = 5; MapType mymap(n, hashing_func(), key_equal_fn()); Also wonder why this type of definition would work, i.e., specifying 5 in the first parameter. This way of definition seems to be missing in the unordered_map API??
Anyone knows what went wrong here? Thanks!
UPDATE: as pointed out, string is an internal class that has a built-in hash function available. So I rephrased the question to use a customized type here.