2010-10-15 132 views
0

我有以下代碼實現哈希表的

#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

回答

5

你要看看每個編譯錯誤並修復它們一次一個。我們無法獲取代碼文件並修復每個錯誤。學習生活,甚至有時候愛你的編輯是虐待配偶是過程的一部分。

+0

特別是當多個看似無法解讀和無關的錯誤的行由單個丟失分號導致。 :-) – Konrad 2010-10-15 15:36:49

+0

解密模板錯誤是一種非常成熟的技巧,而海報似乎是C++的新手。至少,指出錯誤消息的相關部分可能會幫助他/她排除下一次編譯器吐出的嘔吐物! – 2010-10-15 15:44:05

4

所有的錯誤都與您沒有將正確的模板參數傳遞給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類不同,因爲它們是而不是標準。該標準的下一個版本包含標準散列容器。同時,你必須編寫非可移植的代碼!