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

时间:2011-07-29 13:46:10

标签: 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;
}

如您所见,我正在使用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 /本地/包含/谷歌/ dense_hash_map:104:0,                    来自../src/Main.cpp:2:   /usr/local/include/google/sparsehash/densehashtable.h:在会员中   function'bool google :: dense_hashtable :: KeyInfo :: equals(const key_type&amp ;,,   const key_type&amp;)const [with Value = std :: pair,Key = int,HashFcn = std :: tr1 :: hash,ExtractKey =   谷歌:: dense_hash_map,   eqstr&gt; :: SelectKey,SetKey = google :: dense_hash_map,eqstr&gt; :: 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&amp;,const     key_type&amp;)const [with Value = std :: pair,Key     = int,HashFcn = std :: tr1 :: hash,ExtractKey =     谷歌:: dense_hash_map,     eqstr&gt; :: SelectKey,SetKey = google :: dense_hash_map,eqstr&gt; :: SetKey,EqualKey = eqstr,Alloc =     谷歌:: 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)实例化[含值=     std :: pair,Key = int,HashFcn =     std :: tr1 :: hash,ExtractKey = google :: dense_hash_map,eqstr&gt; :: SelectKey,SetKey =     谷歌:: dense_hash_map,     eqstr&gt; :: SetKey,EqualKey = eqstr,Alloc =     谷歌:: libc_allocator_with_realloc     ,google :: dense_hashtable :: const_reference = const std :: pair&amp;]'/ usr / local / include / google / dense_hash_map:298:5:
    实例化自'void google :: dense_hash_map :: set_empty_key(google :: dense_hash_map :: key_type&amp;)[使用Key = int,T = const     char *,HashFcn = std :: tr1 :: hash,EqualKey = eqstr,Alloc =     谷歌:: 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:错误:     来自'google :: dense_hashtable,int,std :: tr1 :: hash,google :: dense_hash_map,eqstr的无效转换,     谷歌:: libc_allocator_with_realloc

         
      

:: SelectKey,eqstr,       谷歌:: libc_allocator_with_realloc       ,google :: dense_hash_map,       eqstr,google :: libc_allocator_with_realloc&gt; &gt; :: SetKey,eqstr,       谷歌:: libc_allocator_with_realloc       ,eqstr,google :: libc_allocator_with_realloc&gt; &gt; :: 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的无效转换,       谷歌:: libc_allocator_with_realloc       :: SelectKey,eqstr,       谷歌:: libc_allocator_with_realloc       ,google :: dense_hash_map,       eqstr,google :: libc_allocator_with_realloc&gt; &gt; :: SetKey,eqstr,       谷歌:: libc_allocator_with_realloc       ,eqstr,google :: libc_allocator_with_realloc&gt; &gt; :: key_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结构以容纳新的数据类型...... bangs离开办公桌

1 个答案:

答案 0 :(得分:3)

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