哈希表的实现

时间:2010-10-15 15:26:18

标签: c++ hash

我有以下代码

#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 ==========

请帮助

2 个答案:

答案 0 :(得分:5)

您必须查看每个编译器错误并一次修复一个。我们无法获取代码文件并修复每个错误。学会生活,甚至偶尔喜欢虐待配偶,这是你的编纂者是这个过程的一部分。

答案 1 :(得分:4)

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

  

c:\ program files \ microsoft visual studio 10.0 \ vc \ include \ hash_map(26):错误C2903:'rebind':符号既不是类模板也不是函数模板            c:\ program files \ microsoft visual studio 10.0 \ vc \ include \ xhash(170):参见类模板实例化'std :: _ Hmap_traits&lt; _Kty,_Ty,_Tr,_Alloc,_Mfl&gt;'被编译            的            [                _Kty = const char *,                _Ty = INT,                _Tr =标准::散列,                的 _Alloc =等于下,                _Mfl =真            ]

修改

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