C++:hashtable - 为什么不编译?

Eam*_*orr 5 c++ compiler-errors hashtable

我有以下C++代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用Google的sparsehash库实现哈希表.

我使用整数作为键和字符串作为值.

但我不断得到以下编译错误,我无法深究:

make all building file:../ src/Main.cpp Invoking:GCC C++ Compiler 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"在/ usr/local/include/google/dense_hash_map中包含的文件:104:0,来自../src/Main .cpp:2:/usr/local/include/google/sparsehash/densehashtable.h:在成员函数'bool google :: dense_hashtable :: KeyInfo :: equals(const key_type&,const key_type&)const [with Value = std ::对,答案= INT,HashFcn =标准:: TR1 ::散列,ExtractKey =谷歌:: dense_hash_map,eqstr> :: selectKey元素,setkey的谷歌= :: dense_hash_map,eqstr> :: setkey的,EqualKey = eqstr,的Alloc =谷歌: :libc_allocator_with_realloc

,key_type = int]':/
usr/local/include/google/sparsehash/densehashtable.h:1306:32 :从'bool google :: dense_hashtable :: equals(const key_type&,const key_type&)const [实例化] [带有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 =谷歌:: libc_allocator_with_realloc,为key_type = INT] '/usr/local/include/google/sparsehash/densehashtable.h:514:5:
从实例化'无效谷歌:: dense_hashtable :: set_empty_key(谷歌:: dense_hashtable ::为const_reference)使用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 =谷歌:: libc_allocator_with_realloc,谷歌:: dense_hashtable :: =为const_reference常量的std ::对&] '的/ usr /本地/包含/谷歌/ dense_hash_map:298:5:
从实例化'无效谷歌:: dense_hash_map :: set_empty_key (goog 勒:: dense_hash_map ::为key_type&)[用密钥= INT,T =常量字符*,HashFcn =标准:: TR1 ::散列,EqualKey = eqstr,的Alloc =谷歌:: libc_allocator_with_realloc,谷歌:: dense_hash_map ::为key_type = INT ]'../src/Main.cpp:21:25:从这里实例化/usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误:从'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_with_realloc>> ::为key_type '到' const的char*'/ usr/local/include/google/sparsehash/densehashtable.h:1293:39:error:初始化'bool eqstr :: operator()的参数1(const char*,const char*)const'/ usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误:无效转换来自'google :: dense_hashtable,int,std :: tr1 :: hash,google :: dense_hash_map,eqstr,google :: libc_allocator_with_realloc :: selectKey元素,eqstr,谷歌:: libc_allocator_with_realloc,谷歌:: dense_hash_map,eqstr,谷歌:: libc_allocator_with_realloc>> :: setkey的,eqstr,谷歌:: libc_allocator_with_realloc,eqstr,谷歌:: libc_allocator_with_realloc>> :: key_type的'到'为const char*'/usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误:初始化'bool eqstr :: operator()的参数2(c onst char*,const char*)const'make :* [src/Main.o]错误1

它似乎非常冗长,我无法理解它.

我应该补充说,当我使用字符串作为键和整数作为值时,它可以正常工作:

dense_hash_map<const char*, int, hash<const char*>, eqstr> months;
...
months["february"] = 2;   //works fine
Run Code Online (Sandbox Code Playgroud)

有人有任何想法吗?

提前谢谢了,


编辑:现在就搞定了!

#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;
}
Run Code Online (Sandbox Code Playgroud)

完全忘了编辑eqstr结构来容纳新的数据类型... 刘海离开桌子

Ale*_*ler 3

正如您自己指出的,如果您使用const char*as 密钥,它就会起作用。哈希图确实需要一个哈希函数来将键哈希到存储桶中,并需要一个比较函数来在存储桶内建立严格的弱排序 - 所有这些都是针对键类型的,只是存储值类型!因此,为了使其工作,请定义一个比较函数int(我不知道 是否const int可以google::dense_hash_map,我认为应该是int)。