源代码来自sgi-2.91版本stl_hash_map.h
文章目录
unordered_map总述
先了解hash表
(1)底层实现完全由hash表代替,最开始叫做hash_map,从实现原理来说unordered_map应该被称为容器适配器
(2)对底层hash表来说,每个节点都有一个键值(key)和实值(value)
(3)hash表没有自动排序,所以相对于map,unordered_map没有自动排序
(4)unordered_map适合用于只查找元素的情况
unordered_map类
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class Value, class HashFcn = hash<Value>, //hash函数来对元素编号
class EqualKey = equal_to<Value>, //比较==函数
class Alloc = alloc> //allco分配器
#else
template <class Key, class T, class HashFcn, class EqualKey,
class Alloc = alloc>
#endif
class hash_map
{
private:
typedef hashtable<pair<const Key, T>, Key, HashFcn, //<pair<const Key, T>传入,说明hash表上链表节点挂载<pair<const Key, T>
select1st<pair<const Key, T> >, EqualKey, Alloc> ht; //申明hash表
ht rep; //创建hash表
public:
typedef typename ht::key_type key_type;
typedef T data_type;
typedef T mapped_type