手写HASHMAP

手写HASHMAP

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. const int MAXN=10010;  
  2. const int HASH=10100;            //需要hash的数的总个数最大值   
  3. struct HASHMAP  
  4. {  
  5.     int head[HASH];  
  6.     int next[MAXN];  
  7.     int size;  
  8.     int state[MAXN];  
  9.     void init()  
  10.     {  
  11.         size=0;  
  12.         memset(head,-1,sizeof(head));  
  13.     }  
  14.     int push(int st)  
  15.     {  
  16.         int i,h=st%HASH;  
  17.         for(i=head[h];i!=-1;i=next[i])  
  18.            if(state[i]==st)  
  19.              return i;  
  20.         state[size]=st;  
  21.         next[size]=head[h];  
  22.         head[h]=size++;  
  23.         return size-1;  
  24.     }  
  25. }hm;  

以上代码实现功能与下面一样,但速度快许多:

 

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. map<int,int>mp;  
  2. int tol=0;  
  3. int insert(int x)  
  4. {  
  5.     if(mp.find(x)==mp.end())mp[x]=tol++;  
  6.     return mp[x];  
  7. }  
posted @ 2017-03-02 10:49  lupeng2010  阅读(554)  评论(0编辑  收藏  举报