C++编程-使用rw_hashmap实现hashmap一例

      目前网上能搜索到的关于C++中实现hashmap的例子很少,今天斗胆来个例子,供初学STL的人一览。
      STL中目前还没有hashmap的实现,这里使用的rw_hashmap是Rogue wave的实现.不过它已经集成到sgi stl中。我在HP-UX B.11.23中看到aCC编译器已经将其集成进来。
      言归正传,rw_hashmap的原型如下:
      rw_hashmap<K,V,Hash,EQ,Allocator> map;
      其中K为key,V为value,Hash为hash函数,EQ为key值等号重载,Allocator为分配器。
  我这里实现的是K->string,V->int.
      1.设计第三个参数Hash
              Hash必须提供一个在类型K的元素上进行hash的public函数:unsigned long operator()(const K& x) const;
              这里使用ELF hash函数:
             #include <rw/tvhdict.h>
             #include <rw/cstring.h>
             #include <utility>
              #include <iostream>
              using namespace std;

             //  ELF Hash Function
            inline unsigned  long  ELFHash(const char   * str)
             {
                       unsigned  int  hash  =   0 ;
                      unsigned  int  x     =   0 ;

                       while  ( * str)
                       {
                                 hash  =  (hash  <<   4 )  +  ( * str ++ );
                                 if  ((x  =  hash  &   0xF0000000L )  !=   0 )
                                 {
                                         hash  ^=  (x  >>   24 );
                                        hash  &=   ~ x;
                                 }
                       }

         return  (hash  &   0x7FFFFFFF );
         }
      
      class ELF_HASH{
        public:
                unsigned long operator()(const std::string& x) const
                {
                     return ELFHash(x.c_str());
                 }
          };
       
      2.设计第四个参数EQ:
               EQ需要如下public函数:bool operator()(const K& x, const K& y) const;当x与y相等时返回true.         
              class STRING_EQUAL{
               public:
                     bool operator()(const string& x, const string& y) const
                     {
                               return (x==y);
                     }
                  };      
         3.现在可以完整的定义了:
          typedef rw_hashmap<string,int,ELF_HASH,STRING_EQUAL,std::allocator<pair<string,int> > > mapDictNameId;
          4.下面是测试代码,有点粗糙,主要是从一堆代码的类中提出来的,懒得继续封装:
         mapDictNameId m_mapDictNameId;
         typedef mapDictNameId::iterator Iterator;
         typedef mapDictNameId::value_type Value_type;

void setNameKey(const string& sFieldName, int nId)
{
    string sTemp = sFieldName;
    m_mapDictNameId.insert(Value_type(sTemp, nId));
    return;
}

int getIdByName(const string& sFieldName)
{
    string sTemp = sFieldName;
    Iterator iter2;
    iter2 = m_mapDictNameId.find(sTemp);
    if (iter2 != m_mapDictNameId.end())
   {
       return (*iter2).second;
   }
  else
       return -1;
}

int main()
{
        setNameKey("NAME1",10);
        setNameKey("NAME2",11);
        int id;
        id=getIdByName("NAME1");
        cout<<"id:"<<id<<endl;                 
}

输出结果为:
          id:10

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值