std::unorder_hashmap分析

 
VS2010下是基于桶实现的;
下面就以operator []来看;
                  mapped_type   operator  [](  key_type &&  _Keyval  )
                                {                // find element matching _Keyval or insert with default mapped
                                  iterator   _Where  this ->  lower_bound  ( _Keyval  );
                                  if  ( _Where  ==  this ->  end  ())
                                                  _Where   this  ->  insert (
                                                                  _STD   pair  <  key_type mapped_type  >(
                                                                                  _STD   move  (  _Keyval ),
                                                                                  mapped_type  ())). first  ;
                                  return  ((* _Where  ).  second );
                                }
先要根据key获取key可能所在的bucket:
                  iterator   lower_bound  (  const   key_type   _Keyval  )
                                {                // find leftmost not less than _Keyval in mutable hash table
                                  size_type   _Bucket  _Hashval (  _Keyval  );
                                  for  ( iterator  _Where  =  _Begin  ( _Bucket  );
                                                  _Where  !=  _End  (  _Bucket ); ++  _Where  )
                                                  if  (! this  ->  comp (  this  -> _Kfn  (*  _Where ),  _Keyval  ))
                                                                  return  ( this  ->  comp (  _Keyval  ,
                                                                                  this  -> _Kfn  (*  _Where )) ?  end  () :  _Where  );
                                  return  ( end  ());
                                }
_Hashval获得所在的桶,里面会根据hashfun得出hashval并与桶的个数做与运算得到所在的桶;
然后遍历桶里的元素,桶其实是一个vector,comp这个比较函数,如果相等返回false,所以如果查到就返回_Where,查不到就返回end();
如果查找到不为空的话,就返回迭代器的val引用;
如果查找不到,就插入:插入的时候还用的move,暂时木有搞明白move语意的意义,下篇博文再分析下。
unordered_map的find方法是之前分析过的,因为[]的时候先会查找,都是用了lower_bound函数。
另外hashfun比较简单就不再分析了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值