首页 文章

C:哈希表 - 为什么不编译?

提问于
浏览
5

我有以下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;
}

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

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

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

make all Building文件:../ src / Main.cpp调用:GCC 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“在/ 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: :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:从'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_ma p,eqstr> :: SetKey,EqualKey = eqstr,Alloc = google :: libc_allocator_with_realloc,key_type = int]'/usr/local/include/google/sparsehash/densehashtable.h:514:5:从'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:从'void google :: dense_hash_map :: set_empty_key(google :: dense_hash_map :: key_type&)实例化[使用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:从这里实例化/usr/local/include/google/sparsehash/densehashtable.h:1293: 39:错误:inval id转换来自'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:错误:初始化'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,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 >> :: ke y_type'到'const char *'/usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误:初始化'bool eqstr :: operator()的参数2(const 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

有人有任何想法吗?

提前谢谢了,


编辑:现在就搞定了!

#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;
}

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

1 回答

  • 3

    正如您自己指出的那样,如果您使用 const char* 作为关键字,它会起作用 . hashmap确实需要散列函数来将键散列到桶和比较函数以在桶内 Build 严格的弱排序 - 所有这些都是密钥类型,值类型只是存储!所以为了使它工作,为 int 定义一个比较函数(我不知道 const int 是否适合 google::dense_hash_map ,我认为它应该是 int ) .

相关问题