unordered_map的使用特性
map,.unordered_map, hash_map的比较https://blog.csdn.net/u014209688/article/details/95366594
1.unordered_map的存储结构
hashtable是可能存在冲突的(多个key通过计算映射到同一个位置),在同一个位置的元素会按顺序链在后面。
把这个位置称为一个bucket
2.桶子个数的增长规律
template<typename K, typename V,typename A, typename Ex, typename Eq,typename H1, typename H2, typename H, typename RP,bool c, bool ci, bool u>voidhashtable<K, V, A, Ex, Eq, H1, H2, H, RP, c, ci, u>::m_rehash(size_type n){node** new_array = m_allocate_buckets(n);try{for (size_type i = 0; i < m_bucket_count; ++i)while (node* p = m_buckets[i]){size_type new_index = this->bucket_index(p, n); // 重新计算桶编号m_buckets[i] = p->m_next;p->m_next = new_array[new_index]; // 将当前的index插入到p的后面new_array[new_index] = p; // 将p作为桶子的表头}m_deallocate_buckets(m_buckets, m_bucket_count); // 释放原来的桶子的内存m_bucket_count = n; // 更新桶子个数m_buckets = new_array; // 修改桶子指针}catch(...){// A failure here means that a hash function threw an exception.// We can't restore the previous state without calling the hash// function again, so the only sensible recovery is to delete// everything.m_deallocate_nodes(new_array, n);m_deallocate_buckets(new_array, n);m_deallocate_nodes(m_buckets, m_bucket_count);m_element_count = 0;__throw_exception_again;}}
为什么unordered_map比hash_map性能更好呢?
hash_map的源码如下
// Re-initialise the hash from the values already contained in the list.void rehash(std::size_t num_buckets){if (num_buckets == num_buckets_)return;num_buckets_ = num_buckets;BOOST_ASIO_ASSERT(num_buckets_ != 0); iterator end_iter = values_.end();// Update number of buckets and initialise all buckets to empty.bucket_type* tmp = new bucket_type[num_buckets_];delete[] buckets_;buckets_ = tmp;for (std::size_t i = 0; i < num_buckets_; ++i)buckets_[i].first = buckets_[i].last = end_iter;...}
每次都要重新申请内存,申请内存的时间造成了大量的浪费。。
3.map与unordered_map的性能比较
map的空间复杂度O(n)
插入,查找,删除的空间复杂度均为O(log(n))
unordered_map:
底层是HashTable和桶,当桶的个数不够时会加倍
插入 查找 删除 O(1)复杂度
当内存较小而时间不限制的时候使用map
当内存不限制而时间要求很高的时候用unordered_map
unordered_map的使用特性相关推荐
- STL中的map、unordered_map、hash_map
转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...
- C++11 unordered_map详细介绍
整理的算法模板合集: ACM模板 目录: 1.介绍 1.1 特性 2. 模版 2.1 迭代器 3. 功能函数 3.1 构造函数 3.2 容量操作 3.2.1 size 3.2.2 empty 3.3 ...
- C++_STL——unordered_map、unordered_multimap、unordered_set、unordered_multiset
C++_STL--unordered_map.unordered_multimap.unordered_set.unordered_multiset 参考:cplusplus 这几种都是拉链法所写的, ...
- 原 c++中map与unordered_map的区别
c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...
- 收藏——CodeProject - 使用特性(attributes)和激活机制来实现工厂模式
收藏--CodeProject - 使用特性(attributes)和激活机制来实现工厂模式 分类: .NetDelphi2005-03-24 09:57496人阅读评论(0)收藏举报 原文 http ...
- unordered_map源码解析和个人理解
参考链接 https://blog.csdn.net/ddkxddkx/article/details/6555754 还有这个源码解析https://zrj.me/archives/1248 1.下 ...
- 【C++】unordered_map 使用方法
1 #include <iostream> 2 #include <unordered_map> 3 #include <map> 4 #include <s ...
- C++语言map和unordered_map的下标操作
C++语言map和unordered_map的下标操作 C++语言map和unordered_map的下标操作
- 算法提高课-搜索-双向广搜 AcWing 190. 字串变换:bfs、双向bfs、queue和unordered_map
题目分析 来源:acwing 分析: 双向广搜主要用在最小步数模型(也称状态图模型)里面,这里整个状态空间一般是指数级别的,用双向广搜可以极大地提高运行效率. 双向广搜,顾名思义,就是从起点和终点都进 ...
最新文章
- JS计算两个时间相差多久,相差年,月,日,小时,分钟
- PHP 开发中的外围资源性能分析(一)
- 如何解决Android 5.0中出现的警告:Service Intent must be explicit
- PostgreSQL 常见操作
- CCNA 学习笔记(四)--路由协议(RIP)
- python 科学计算设计_用Python做科学计算 pdf版
- 阻止xap文件在浏览器中缓存
- Amazon电商数据分析——数据获取
- mysql数据库调试_mysql数据库调试
- 003_ElasticSearch详解与优化设计
- php 单词替换,单词替换 - Shiyin's note
- 有重复组合公式及其证明方法
- JVM学习笔记(13) 垃圾回收-相关概念
- linux缺页异常,操作系统缺页异常
- QQ2005beta3新功能图片抢先看!(转)
- 全球光刻机龙头是怎样炼成的
- php快手开发平台api签名
- 10分钟在局域网中搭建一个课程点播+直播系统
- 汉诺塔python3函数编写和过程分析
- “金三银四”春招指南!mysql修改表字段名称