std::unorder_hashmap分析
来源:互联网 发布:淘宝网演出服装 编辑:程序博客网 时间:2024/06/01 08:52
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 0
- std::unorder_hashmap分析
- std::map源码分析
- vs2013 std::sort 分析
- std::function源码分析
- C++ STL std::sort分析
- std::list<>::sort()排序分析
- std::less()用法及分析分析
- C++11尝鲜:std::move和std::forward源码分析
- C++11尝鲜:std::move和std::forward源码分析
- C++11:std::move和std::forward源码分析
- C++11尝鲜:std::move和std::forward源码分析
- C++ std::move和std::forward源码分析
- STL源码分析阅读-------std allocator
- std::string源码探秘和性能分析
- ROS中std::msgs类型分析
- std
- std
- std
- string与char*/char[]的转换
- Java实现定时任务的三种方法
- ZOJ 3713
- Android ListView和ListAdapter
- Oracle SYSTEM 表空间过满的处理(扩展表空间)
- std::unorder_hashmap分析
- CTA图像中肝脏血管增强及肝脏与血管同时分割的方法
- 畅通工程 行电1232最小生成树
- 如何在LLDB下排查message sent to deallocated instance问题
- JS的输入正则验证
- JavaSE基础第一部分:搭建环境与编写第一个程序
- SAP ABAP 用debug方法修改dictionary数据
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- ruby reading record