STL的hasp_map学习笔记

来源:互联网 发布:mysql 多表inner join 编辑:程序博客网 时间:2024/06/10 21:35
 1.HASH函数的声明问题
template <class _Key, class _Tp, class _HashFcn = hash<_Key>,
class _EqualKey = equal_to<_Key>,
class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
class hash_map
{
...
}
也就是说,在上例中,有以下等同关系:
...
hash_map<int, string> mymap;
//等同于:
hash_map<int, string, hash<int>, equal_to<int> > mymap
-------------------------------------------
1.有关hash函数的笔记
struct hash<int> {
size_t operator()(int __x) const { return __x; }
};
原来是个函数对象。在SGI STL中,提供了以下hash函数: 
struct hash<char*>
struct hash<const char*>
struct hash<char>
struct hash<unsigned char>
struct hash<signed char>
struct hash<short>
struct hash<unsigned short>
struct hash<int>
struct hash<unsigned int>
struct hash<long>
struct hash<unsigned long>
 
我的观点:上面的内容还是比较简单的,接着下面的内容非常关键
也就是说,如果你的key使用的是以上类型中的一种,你都可以使用缺省的hash函数。当然你自己也可以定义自己的hash函数。对于自定义变量,你只能如此,例如对于string,就必须自定义hash函数。例如: 
struct str_hash{
size_t operator()(const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return size_t(__h);
}
};
//如果你希望利用系统定义的字符串hash函数,你可以这样写:
struct str_hash{
size_t operator()(const string& str) const
{
return return __stl_hash_string(str.c_str());
}
};
在声明自己的哈希函数时要注意以下几点: 
  1. 使用struct,然后重载operator().
  2. 返回是size_t
  3. 参数是你要hash的key的类型。
  4. 函数是const类型的。

 

直接替换成下面的声明即可:

map<string, string> namemap; 
//改为:
hash_map<string, string, str_hash> namemap
2.有关比较函数的笔记
在map中的比较函数,需要提供less函数。如果没有提供,缺省的也是less< Key> 。在hash_map中,要比较桶内的数据和key是否相等,因此需要的是是否等于的函数:equal_to< Key> 。先看看equal_to的源码: 
//本代码可以从SGI STL
//先看看binary_function 函数声明,其实只是定义一些类型而已。
template <class _Arg1, class _Arg2, class _Result>
struct binary_function {
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};
//看看equal_to的定义:
template <class _Tp>
struct equal_to : public binary_function<_Tp,_Tp,bool>
{
bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
};
如果你使用一个自定义的数据类型,如struct mystruct, 或者const char* 的字符串,如何使用比较函数?使用比较函数,有两种方法. 第一种是:重载==操作符,利用equal_to;看看下面的例子: 
struct mystruct{
int iID;
int len;
bool operator==(const mystruct & my) const{
return (iID==my.iID) && (len==my.len) ;
}
};
这样,就可以使用equal_to< mystruct>作为比较函数了。另一种方法就是使用函数对象。自定义一个比较函数体: 
struct compare_str{
bool operator()(const char* p1, const char*p2) const{
return strcmp(p1,p2)==0;
}
};
有了compare_str,就可以使用hash_map了。 
typedef hash_map<const char*, string, hash<const char*>, compare_str> StrIntMap;
StrIntMap namemap;

------------------
#include   <iostream>  
  #include   <string>  
  #include   <ext/hash_map>  
  using   namespace   std;  
  using   namespace   __gnu_cxx;  
   
  namespace   __gnu_cxx  
  {  
          template<>   struct   hash<const   string>  
          {  
                  size_t   operator()(const   string&   s)   const  
                  {   return   hash<const   char*>()(   s.c_str()   );   }   //   __stl_hash_string  
          };  
          template<>   struct   hash<string>  
          {  
                  size_t   operator()(const   string&   s)   const  
                  {   return   hash<const   char*>()(   s.c_str()   );   }  
          };  
  }  
   
  int   main(   void   )  
  {  
          hash_map<string,int>   test;  
          test["abc"]   =   1;  
          cout   <<   test["abc"]   <<   endl;  
          system(   "pause"   );  
  }
原创粉丝点击