GCC中使用hash_map

GCC中使用hash_map
2009-04-01 21:34

最近学习STL,hash_map就是利用hash表实现的一个map,在查找是具有更快的效率,前提是你使用了适当的hash函数。hash_map不是STL中的一部分,但大多数C/c++的编译器都提供了这个容器。GCC也不例外,我在winds下使用的IDE是code blocks,它使用的编译器就是GCC。因为hash_map不是STL中的一部分,所以使用起来也不是很异样。下面的是我搜索到的一些资料:

【1】

http://zitronensaft.blogspot.com/2008/02/using-hashmap-on-gcc.html

Using hash_map on GCC

If you have tried to use some STL containers with GCC, such as hash_map:

// error: hash_map: No such file or directory












#include <hash_map>













int





 main





()



{
















 // error: ‘hash_map’ is not a member of ‘std’












 std::





hash_map<





int


,





int


>





 hm;












 



 return





 0





;



}









Then you have realized that the code above does not compile. That's because on GCC, hash_map is not regarded as a standard container, but rather as a extension included in the __gnu_cxx namespace . In order to use hash_map and other extended containers with a minimum impact in your code (which is very important if it's intended to be cross-platform), you can use the following solution:

   
   
#ifdef __GNUC__ #include <ext/hash_map> #else #include <hash_map> #endif namespace std { using namespace __gnu_cxx ; } int main () { std:: hash_map< int , int > hm; return 0 ; }

Hope that helps.

----------------------------------------------------------------------------------------------------------------------------------

【2】

http://www.velocityreviews.com/forums/t280027-why-wont-this-hashmap-compile.html

Hi,

I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It
will compile if I use <map> but I get a bunch of template compile errors
when I change it to hash_map. Any suggestions? My program and the errors
are below...

#include <ext/hash_map>
#include <string>

using namespace std;

__gnu_cxx::hash_map<string, int> storage;

int main()
{
pair<string, int> item("blah", 1);
storage.insert(item);
}

When compiled I get the following:
g++ hm.cpp -o hm
/usr/include/c++/3.2.2/ext/stl_hashtable.h: In member function `size_t
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const [with _Val =
std:air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std:air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]':
/usr/include/c++/3.2.2/ext/stl_hashtable.h:522: instantiated from `size_t
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::_M_bkt_num(const _Val&, unsigned int) const [with _Val =
std:air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std:air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]'
/usr/include/c++/3.2.2/ext/stl_hashtable.h:887: instantiated from `void
__gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
_Alloc>::resize(unsigned int) [with _Val = std:air<const std::string,
int>, _Key = std::string, _HashFcn = __gnu_cxx::hash<std::string>,
_ExtractKey = std::_Select1st<std:air<const std::string, int> >, _EqualKey
= std::equal_to<std::string>, _Alloc = std::allocator<int>]'
/usr/include/c++/3.2.2/ext/stl_hashtable.h:381: instantiated from
`std:air<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey,
_EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::insert_unique(const _Val&) [with _Val =
std:air<const std::string, int>, _Key = std::string, _HashFcn =
__gnu_cxx::hash<std::string>, _ExtractKey = std::_Select1st<std:air<const
std::string, int> >, _EqualKey = std::equal_to<std::string>, _Alloc =
std::allocator<int>]'
/usr/include/c++/3.2.2/ext/hash_map:171: instantiated from
`std:air<__gnu_cxx::hashtable<std:air<const _Key, _Tp>, _Key, _HashFcn,
std::_Select1st<std:air<const _Key, _Tp> >, _EqualKey, _Alloc>::iterator,
bool> __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
_Alloc>::insert(__gnu_cxx::hashtable<std:air<con st _Key, _Tp>, _Key,
_HashFcn, std::_Select1st<std:air<const _Key, _Tp> >, _EqualKey,
_Alloc>::value_type&) [with _Key = std::string, _Tp = int, _HashFcn =
__gnu_cxx::hash<std::string>, _EqualKey = std::equal_to<std::string>, _Alloc
= std::allocator<int>]'
hm.cpp:14: instantiated from here
/usr/include/c++/3.2.2/ext/stl_hashtable.h:517: no match for call to `(const
__gnu_cxx::hash<std::string>) (const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&)'
make: *** [hm] Error 1



Mark

-----------------------------------------------------------------------------------------------------------------------------------

【3】

Re: gcc-3.0.2 + hash_map<string,int> compilation problems

  • From: Stefan Olsson <stefan at noname4us dot com>
  • To: Matthew Sundling <sundlm at cs dot wisc dot edu>
  • Cc: libstdc++ at gcc dot gnu dot org
  • Date: Mon, 08 Apr 2002 23:42:45 +0200
  • Subject: Re: gcc-3.0.2 + hash_map<string,int> compilation problems
  • Organization: Noname4us
  • References: <Pine.GSO.4.21.0204081557530.14231-100000@nova5.cs.wisc.edu>

Hi!







I have had the same issue and solved it by declaring:







#include <string>



#include <ext/hash_map>







namespace __gnu_cxx



{



        template<> struct hash< std::string >



        {



                size_t operator()( const std::string& x ) const



                {



                        return hash< const char* >()( x.c_str() );



                }



        };



}







I guess that the reason for not having this declared in the std::string 



is that the hash* are extensions to the standard. Maybe something like 



the above could be added into the codebase somewhere else?







Hope that helps!







/Stefan







Matthew Sundling wrote:







>I have been trying to compile an extremely simple program that



>uses a hash_map<string, int>.  I can't quit figure out what the



>errors are telling to supply to the STL container to satisfy it.



>



>Thanks for any insight you might have:



>



>[---------------------------------------------------------------]



>My program:



>



>#include <iostream>



>#include <string>



>#include <ext/hash_map>



>



>int main () {



>  std::hash_map<std::string, int > str_hash;



>  str_hash["text"] = 1;



>  std::cout << str_hash["test"] << std::endl;



>  return 0;



>}



>



>



>[---------------------------------------------------------------]



>My gcc output:



>



>/s/unsup/include/g++-v3/ext/stl_hashtable.h: In member function



>`size_t std::hashtable<_Val, _Key, _HashFcn, _ExtractKey, 



>_EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, unsigned int) const 



>[with _Val = std::pair<const std::string, int>, 



>_Key = std::string,



>_HashFcn = std::hash<std::string>, 



>_ExtractKey = std::_Select1st<std::pair<const std::string, int>



>



>>, 



>>



>_EqualKey = std::equal_to<std::string>,



>_Alloc = std::allocator<int>]':



>



>/s/unsup/include/g++-v3/ext/stl_hashtable.h:512:   instantiated



>from `size_t std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,



>_EqualKey, _Alloc>::_M_bkt_num(const _Val&, unsigned int) const



>[with _Val = std::pair<const std::string, int>, 



>_Key = std::string, _HashFcn = std::hash<std::string>,



>_ExtractKey = std::_Select1st<std::pair<const std::string, int>



>



>>, 



>>



>_EqualKey = std::equal_to<std::string>, 



>_Alloc = std::allocator<int>]'



>



>/s/unsup/include/g++-v3/ext/stl_hashtable.h:856:   instantiated



>from `void std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,



>_EqualKey, _Alloc>::resize(unsigned int) 



>[with _Val = std::pair<const std::string, int>, 



>_Key = std::string, 



>_HashFcn = std::hash<std::string>, 



>_ExtractKey = std::_Select1st<std::pair<const std::string, int>



>



>>, 



>>



>_EqualKey = std::equal_to<std::string>, 



>_Alloc = std::allocator<int>]'



>



>/s/unsup/include/g++-v3/ext/stl_hashtable.h:669:   instantiated



>from `_Val& std::hashtable<_Val, _Key, _HashFcn, _ExtractKey,



>_EqualKey, _Alloc>::find_or_insert(const _Val&) 



>[with _Val = std::pair<const std::string, int>, 



>_Key = std::string, 



>_HashFcn = std::hash<std::string>, 



>_ExtractKey = std::_Select1st<std::pair<const std::string, int>



>



>>, 



>>



>_EqualKey = std::equal_to<std::string>, 



>_Alloc = std::allocator<int>]'



>



>/s/unsup/include/g++-v3/ext/hash_map:173:   instantiated from



>`_Tp& std::hash_map<_Key, _Tp, _HashFcn, _EqualKey,



>_Alloc>::operator[](typename std::hashtable<std::pair<const _Key,



>_Tp>, _Key, _HashFcn, std::_Select1st<std::pair<const _Key, _Tp>



>



>>, _EqualKey, _Alloc>::key_type&) 



>>



>[with _Key = std::string, 



>_Tp = int, 



>_HashFcn = std::hash<std::string>, 



>_EqualKey = std::equal_to<std::string>, 



>_Alloc = std::allocator<int>]'



>



>src/junk.cpp:7:   instantiated from here



>/s/unsup/include/g++-v3/ext/stl_hashtable.h:507: no match for



>call to `(const std::hash<std::string>) (const std::basic_string<char, 



>   std::char_traits<char>, std::allocator<char> >&)'



>



>



>Thanks!



>Matt



>



>------------------



>Matthew Sundling



>sundlm@cs.wisc.edu



>



>



>



看了上面三个资料后,我想你可以像使用一个map一样使用hash_map了。







转自 http://hi.baidu.com/lr02/blog/item/7aca821c2294f78787d6b6a5.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值