实现哈希表的

问题描述:

我有以下代码实现哈希表的

#include <stdio.h> 
#include <iostream> 
#include <algorithm> 
#include <string.h> 
#include <hash_map> 
#include <string> 
#include <iterator> 
#include <ostream> 
using namespace std; 
struct Equal 
{ 

    bool operator()(const char *s1,const char *s2)const 
     { 
      return std::strcmp(s1,s2)==0; 
     } 

}; 
    typedef std::hash_multimap<const char*,int,hash<const char*>,Equal>map_type; 
    void lookup(const map_type&Map,const char *str){ 
     cout<<str<<":"; 
     pair<map_type::const_iterator ,map_type::const_iterator>p=Map.equal_range(str); 
     for (map_type::const_iterator i=p.first;i!=p.second;++i) 
      cout << (*i).second<<" "; 



    } 
int maain(){ 

    map_type m; 
    m.insert(map_type::value_type("H", 1)); 
m.insert(map_type::value_type("H", 2)); 
m.insert(map_type::value_type("C", 12)); 
    m.insert(map_type::value_type("C", 13)); 
    m.insert(map_type::value_type("O", 16)); 
    m.insert(map_type::value_type("O", 17)); 
    m.insert(map_type::value_type("O", 18)); 
    m.insert(map_type::value_type("I", 127)); 
    lookup(M,"I"); 
    lookup(M,"0"); 
    lookup(M,"Rn"); 


    return 0; 
} 

但这里是错误

1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled 
1>   with 
1>   [ 
1>    _Kty=const char *, 
1>    _Ty=int, 
1>    _Tr=std::hash<const char *>, 
1>    _Alloc=Equal, 
1>    _Mfl=true 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\hash_map(273) : see reference to class template instantiation 'std::_Hash<_Traits>' being compiled 
1>   with 
1>   [ 
1>    _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true> 
1>   ] 
1>   c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(22) : see reference to class template instantiation 'stdext::hash_multimap<_Kty,_Ty,_Tr,_Alloc>' being compiled 
1>   with 
1>   [ 
1>    _Kty=const char *, 
1>    _Ty=int, 
1>    _Tr=std::hash<const char *>, 
1>    _Alloc=Equal 
1>   ] 
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'rebind' : is not a member of 'Equal' 
1>   c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(11) : see declaration of 'Equal' 
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2143: syntax error : missing ';' before '<' 
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'other' : is not a member of '`global namespace'' 
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(27): error C2238: unexpected token(s) preceding ';' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2039: 'bucket_size' : is not a member of 'std::hash<_Kty>' 
1>   with 
1>   [ 
1>    _Kty=const char * 
1>   ] 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2065: 'bucket_size' : undeclared identifier 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2039: 'allocator_type' : is not a member of 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' 
1>   with 
1>   [ 
1>    _Kty=const char *, 
1>    _Ty=int, 
1>    _Tr=std::hash<const char *>, 
1>    _Alloc=Equal, 
1>    _Mfl=true 
1>   ] 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2146: syntax error : missing ',' before identifier 'allocator_type' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2065: 'allocator_type' : undeclared identifier 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(186): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(187): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(188): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(189): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(190): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(191): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(192): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(193): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(197): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2955: 'std::list' : use of class template requires template argument list 
1>   c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2146: syntax error : missing ';' before identifier 'iterator' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C3254: 'std::_Hash<_Traits>' : class contains explicit override 'type' but does not derive from an interface that contains the function declaration 
1>   with 
1>   [ 
1>    _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true> 
1>   ] 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2838: 'type' : illegal qualified name in member declaration 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2208: 'std::iterator' : no members defined using this type 
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): fatal error C1903: unable to recover from previous error(s); stopping compilation 
1> 
1>Build FAILED. 
1> 
1>Time Elapsed 00:00:01.96 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

请帮助

+8

'int maain(){'不会帮助太多 – Konrad 2010-10-15 15:30:31

+0

是不是你带着昵称david-dav的家伙的转世......我不记得确切吗? – Andrey 2010-10-15 15:37:02

+1

我强烈建议您使用整数获得散列表代码。基本工作后,然后转换为模板。 – 2010-10-15 15:49:38

你要看看每个编译错误并修复它们一次一个。我们无法获取代码文件并修复每个错误。学习生活,甚至有时候爱你的编辑是虐待配偶是过程的一部分。

+0

特别是当多个看似无法解读和无关的错误的行由单个丢失分号导致。 :-) – Konrad 2010-10-15 15:36:49

+0

解密模板错误是一种非常成熟的技巧,而海报似乎是C++的新手。至少,指出错误消息的相关部分可能会帮助他/她排除下一次编译器吐出的呕吐物! – 2010-10-15 15:44:05

所有的错误都与您没有将正确的模板参数传递给hash_multimap有关。看看hash_multimap's documentation。最后一个参数,Èqual`应该是分配,而不是一个函数对象,检查出的第一个错误:

c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled with [ _Kty=const char *, _Ty=int, _Tr=std::hash, _Alloc=Equal, _Mfl=true ]

编辑

如果你已经看了SGI's hash_multimap documentation,你会发现,供应商之间的hash_maphash_multimap类不同,因为它们是而不是标准。该标准的下一个版本包含标准散列容器。同时,你必须编写非可移植的代码!