-1

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.

2

2 Answers 2

0

An unordered_map for string as key is handled internally. You don't have to define hash functions for string as keys.

If you want to pass a class or structure as a key. Then you have to define a hash function for it to calculate hash index for keys where collisions are minimum. To find the structure or class object using .find, overload bool operator == in the structure or class that you are using.

Example:

#include <iostream> #include<unordered_map> int main() { std::unordered_map<std::string, int> myMap; myMap.insert({"asd", 1}); myMap.insert({"qwe", 2}); std::string input; std::cout<<"Enter String: "; std::cin>>input; std::unordered_map<std::string, int>::iterator it = myMap.find(input); if(it != myMap.end()) std::cout<<"Found"; else std::cout<<"Not Found"; } 
Sign up to request clarification or add additional context in comments.

Comments

-1

your custom type of function "key_equal_fn" is [string, string], which is not match with "text2cnt"

If you want do it in a fully-specialized template way,how about this:

template <typename T> struct key_equal_fn { bool operator()(const T& t1, const T& t2) const { return t1==t2; } }; template<> struct key_equal_fn<string> { bool operator()(const string& t1, const string& t2) const { return !(t1.compare(t2)); } }; unordered_map<string, string, hashing_func, key_equal_fn<string> > text2cnt; unordered_map<string, string, hashing_func, key_equal_fn<string> >::iterator iter; 

2 Comments

well, I guess the value type doesn't really matter here. I wanted to use <string, int>, although the cited link uses <string, string>. Either way, the key_equal_fn should be dealing with string and string, because they're the keys from two items in the hash map.
you mean you want do a fully-specialized template? i will reedit it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.