C ++: hashtable - Why is this not compiling?

I have the following C ++ code:

#include <iostream> #include <google/dense_hash_map> #include <string.h> using google::dense_hash_map; // namespace where class lives by default using std::cout; using std::endl; using std::tr1::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS struct eqstr { bool operator()(const char* s1, const char* s2) const { return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0); } }; int main(void){ dense_hash_map<const int, const char*, hash<const int>, eqstr> months; months.set_empty_key(0); months[1234] = "1234"; cout << "1234:" << months[1234] << endl; } 

As you can see, I am implementing a hash table using the Google sparsehash library.

I use integers as a key and strings as a value.

But I keep getting the following compilation error, which I cannot get at the bottom:

make the whole building file: ../ src / Main.cpp Call: GCC compiler C ++ g ++ -O0 -g3 -Wall -c -fmessage-length = 0 -MMD -MP -MF "src / Main.d" -MT "src / Main.d" -o "src / Main.o" "../src/Main.cpp" In the file included in / usr / local / include / google / dense_hash_map: 104: 0, from .. / src / Main.cpp: 2: /usr/local/include/google/sparsehash/densehashtable.h: In the member function 'bool google :: dense_hashtable :: KeyInfo :: equals (const key_type &, const key_type &) const [co value = std :: pair, Key = int, HashFcn = std :: tr1 :: hash, ExtractKey = Google :: dense_hash_map, eqstr> :: SelectKey, SetKey = google :: dense_hash_map, eqstr> :: SetKey, EqualKey = eqstr , Alloc = Google :: libc_allocator_with_realloc

key_type = int]: /usr/local/include/google/sparsehash/densehashtable.h:1306:32:
instance from 'bool google :: dense_hashtable :: equals (const key_type &, const key_type &) const [with value = std :: pair, Key = int, HashFcn = std :: tr1 :: hash, ExtractKey = Google :: dense_hash_map , eqstr> :: SelectKey, SetKey = google :: dense_hash_map, eqstr> :: SetKey, EqualKey = eqstr, Alloc = Google :: libc_allocator_with_realloc, key_type = int] /usr/local/include/google/sparsehash/densehashtable 514: 5:
instance from 'void google :: dense_hashtable :: set_empty_key (google :: dense_hashtable :: const_reference) [with value = std :: pair, Key = int, HashFcn = std :: tr1 :: hash, ExtractKey = google :: dense_hash_map, eqstr> :: SelectKey, SetKey = Google :: dense_hash_map, eqstr> :: SetKey, EqualKey = eqstr, Alloc = Google :: libc_allocator_with_realloc, google :: dense_hashtable :: const_reference = const std :: pair &] / usr / local / include / google / dense_hash_map: 298: 5:
created from 'void google :: dense_hash_map :: set_empty_key (google :: dense_hash_map :: key_type &) [with Key = int, T = const char *, HashFcn = std :: tr1 :: hash, EqualKey = eqstr, Alloc = Google :: libc_allocator_with_realloc, google :: dense_hash_map :: key_type = int] ../ src / Main.cpp: 21:25: instance from here /usr/local/include/google/sparsehash/densehashtable.h:1293:39: error: invalid conversion from 'google :: dense_hashtable, int, std :: tr1 :: hash, google :: dense_hash_map, eqstr, Google :: libc_allocator_with_realloc

:: SelectKey, eqstr, Google :: libc_allocator_with_realloc, google :: dense_hash_map, eqstr, google :: libc_allocator_with_realloc → :: SetKey, eqstr, Google :: libc_allocator_with_realloc :: eqstr usr / local / include / google / sparsehash / densehashtable.h: 1293: 39: error: initializing argument 1 from 'bool eqstr :: operator () (const char *, const char *) const / usr / local / include / google /sparsehash/densehashtable.h:1293:39: error: invalid conversion from 'google :: dense_hashtable, int, std :: tr1 :: hash, google :: dense_hash_map, eqstr, Google :: libc_allocator_with_realloc :: SelectKey, eqstr, Google :: libc_allocator_with_realloc, google :: dense_hash_map, eqstr, google :: libc_allocator_with_realloc → :: SetKey, eqstr, Google :: libc_allocator_with_realloc, eqstr, google :: libc_allocator_wit h_realloc → :: key_type to 'const char * /usr/local/include/google/sparsehash/densehashtable.h:1293:39: error: initializing argument 2 of' bool eqstr :: operator () (const char *, const char *) const make: * [src / Main.o] Error 1

It seems very verbose, and I cannot understand it.

I should add that when I use strings as a key and integers as a value, it works fine:

 dense_hash_map<const char*, int, hash<const char*>, eqstr> months; ... months["february"] = 2; //works fine 

Does anyone have any ideas?

Thank you very much in advance,


Edit: it works now!

 #include <iostream> #include <google/dense_hash_map> #include <string.h> using google::dense_hash_map; // namespace where class lives by default using std::cout; using std::endl; using std::tr1::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS struct eqstr { bool operator()(int s1, int s2) const { return (s1 == s2); //|| (s1 && s2 && strcmp(s1, s2) == 0); } }; int main(void){ dense_hash_map<int, const char*, hash<int>, eqstr> months; months.set_empty_key(0); months[1234] = "1234"; cout << "1234:" << months[1234] << endl; } 

I completely forgot about editing the eqstr structure to accommodate new data types ... bangs head off desk

+6
source share
1 answer

As you yourself indicated, this works if you use const char* as the key. A hash function really needs a hash function for hashing keys to buckets and a comparison function to establish strict weak ordering inside buckets - all this is for the key type, the value type is simply saved! To make it work, define a comparison function for int (I don't know if const int good with google::dense_hash_map , I think it should be int ).

+3
source

Source: https://habr.com/ru/post/893866/


All Articles