我有以下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的散列库实现哈希表。
我使用整数作为键,字符串作为值。
但是,我不断地得到以下编译错误,但我无法找到它的底部:
使所有构建文件:../src/Main.cpp调用: /usr/local/include/google/dense_hash_map:104:0,中包含的文件中的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”从./src/main.cpp:2: /usr/local/include/google/sparsehash/densehashtable.h: In成员函数‘bool google::dense_hashtable::KeyInfo::equals(const key_type&,const key_type&) --具有值=std::偶对,Key = int,HashFcn =std::tr1::散列,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‘:key_type
实例化自‘bool google::dense_hashtable::equals(const key_type&,const key_type&) const,具有值=std::key_type&,Key = int,HashFcn =std::tr1:散列,ExtractKey = google::dense_hash_map,eqstr>::SelectKey,SetKey = google::dense_hash_map,eqstr>::SetKey,EqualKey = eqstr,Alloc = google::libc_allocator_with_realloc,/usr/local/include/google/sparsehash/densehashtable.h:514:5:= key_type
用值=std::google::dense_hashtable::set_empty_key(google::dense_hashtable::const_reference),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,/usr/local/include/google/dense_hash_map:298:5:= const::google::dense_hashtable::const_reference &‘google::dense_hashtable::const_reference
实例化自‘void google::dense_hash_map::set_empty_key(google::dense_hash_map::key_type&) with Key = int,T= const *,HashFcn = std::tr1::hash,EqualKey = eqstr,Alloc = google::libc_allocator_with_realloc,/usr/local/include/google/sparsehash/densehashtable.h:1293:39: google::dense_hash_map::key_type = int‘./src/main.cpp:21:25:从这里实例化google::dense_hash_map::key_type错误:从google::dense_hashtable、int、std::tr1:散列、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,从‘const char*’到‘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,/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结构来容纳新的数据类型.刘海头离开桌子
发布于 2011-07-29 13:51:21
正如您自己指出的,如果您使用const char*
作为密钥,它就会工作。哈希映射确实需要一个散列函数来散列桶的键,并需要一个比较函数来在桶中建立严格的弱排序--对于键类型,值类型只是存储!为此,为int
定义一个比较函数(我不知道const int
是否适合google::dense_hash_map
,我认为应该是int
)。
https://stackoverflow.com/questions/6874129
复制相似问题