C++效率掌握之STL库:unordered_map && unordered_set底层剖析

文章目录

看了前面的底层封装后,其实封装的过程及方法都大差不差,unordered_map && unordered_set 也是如此,所以本篇就简单提及一些细节,具体最详细的一些部分可以去看前面的文章

传送门:C++效率掌握之STL库:list底层剖析及迭代器万字详解
传送门:C++效率掌握之STL库:map && set底层剖析及迭代器万字详解

1.unordered_map、unordered_set的基本结构

🚩unordered_set:

cpp 复制代码
template<class K, class V>
class unordered_set
{
	struct SetKeyOfT
	{
		const K& operator()(const K& key)
		{
			return key;
		}
	};
public:
	typedef typename hash_bucket::HashTable<K, K, SetKeyOfT>::const_iterator iterator;
	typedef typename hash_bucket::HashTable<K, K, SetKeyOfT>::const_iterator const_iterator;


	const_iterator begin() const
	{
		return _ht.begin();
	}

	const_iterator end() const
	{
		return _ht.end();
	}

	pair<const_iterator, bool> insert(const K& key)
	{
		pair<typename hash_bucket::HashTable<K, K, SetKeyOfT>::iterator, bool> ret = _ht.Insert(key);
		return pair<const_iterator, bool>(ret.first, ret.second);
	}
private:
	hash_bucket::HashTable<K, K, SetKeyOfT> _ht;
};

🚩unordered_map:

cpp 复制代码
template<class K, class V>
class unordered_map
{
	struct MapKeyOfT
	{
		const K& operator()(const pair<const K, V>& kv)
		{
			return kv.first;
		}
	};
public:
	typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT>::iterator iterator;
	typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT>::const_iterator const_iterator;

	iterator begin()
	{
		return _ht.begin();
	}

	iterator end()
	{
		return _ht.end();
	}

	const_iterator begin() const
	{
		return _ht.begin();
	}

	const_iterator end() const
	{
		return _ht.end();
	}

	pair<iterator, bool> insert(const pair<K, V>& kv)
	{
		return _ht.Insert(kv);
	}

	V& operator[](const K& key)
	{
		pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
		return ret.first->second;
	}
private:
	hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT> _ht;
};

unordered_set 虽然事 k-k 类型,unordered_mapk-v 类型,但是实际上这两个类共用一个哈希表,准确来说是共用同一个模板类型,unordered_set<K,K>unordered_map<K,pair<K,V>>

2.普通迭代器

cpp 复制代码
template<class K, class T, class Ptr, class Ref, class KeyOfT, class HashFunc>
struct HTIterator
{
	typedef HashNode<T> Node;
	typedef HTIterator<K, T, Ptr, Ref, KeyOfT, HashFunc> Self;
	typedef HTIterator<K, T, T*, T&, KeyOfT, HashFunc> Iterator;

	Node* _node;
	const HashTable<K, T, KeyOfT, HashFunc>* _pht;

	/*HTIterator(Node* node, HashTable<K, T, KeyOfT, HashFunc>* pht)
		:_node(node)
		,_pht(pht)
	{}*/

	HTIterator(Node* node, const HashTable<K, T, KeyOfT, HashFunc>* pht)
		:_node(node)
		, _pht(pht)
	{
	}

	// 普通迭代器时,他是拷贝构造
	// const迭代器时,他是构造
	HTIterator(const Iterator& it)
		:_node(it._node)
		, _pht(it._pht)
	{
	}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	Self& operator++()
	{
		if (_node->_next)
		{
			// 当前桶还没完
			_node = _node->_next;
		}
		else
		{
			KeyOfT kot;
			HashFunc hf;
			size_t hashi = hf(kot(_node->_data)) % _pht->_table.size();
			// 从下一个位置查找查找下一个不为空的桶
			++hashi;
			while (hashi < _pht->_table.size())
			{
				if (_pht->_table[hashi])
				{
					_node = _pht->_table[hashi];
					return *this;
				}
				else
				{
					++hashi;
				}
			}

			_node = nullptr;
		}

		return *this;
	}

	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}

	bool operator==(const Self& s)
	{
		return _node == s._node;
	}
};

typedef HTIterator<K, T, T*, T&, KeyOfT, HashFunc> Iterator 和构造函数是为 Insert 操作准备的,因为涉及到普通迭代器创建 const 迭代器

🔥值得注意的是: 有人说不是可以通过权限转化吗?但是权限转化是只有涉及引用和指针的类型时才会生效,而这里是模板参数之间的转化

3.const迭代器

unordered_set 本身无论是 const 迭代器还是普通迭代器都会被 typedefconst_iterator

对于 unordered_map 来说,key 是不允许改变的,value 是可以改变的,但是如果像 set 那样写的话 keyvalue 都不能修改了,所以直接在 pairkeyconst ,控制 value 即可

4.insert返回值 operator[]

🚩unordered_set:

cpp 复制代码
pair<const_iterator, bool> insert(const K& key)
{
	//return _ht.Insert(key);
	pair<typename hash_bucket::HashTable<K, K, SetKeyOfT>::iterator, bool> ret = _ht.Insert(key);
	return pair<const_iterator, bool>(ret.first, ret.second);
}

🚩unordered_map:

cpp 复制代码
pair<iterator, bool> insert(const pair<K, V>& kv)
{
	return _ht.Insert(kv);
}

V& operator[](const K& key)
{
	pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
	return ret.first->second;
}

这里的转化尤为复杂,一定要看之前文章的详细解析

希望读者们多多三连支持

小编会继续更新

你们的鼓励就是我前进的动力!

相关推荐
煤灰24225 分钟前
简单用c++的类实现的string
java·开发语言·c++
vegetablesssss1 小时前
QGrphicsScen画布网格和QGrphicsItem对齐到网格
c++·qt
chen_song_1 小时前
CUDA的设备,流处理器(Streams),核,线程块(threadblock),线程,网格(‌gridDim),块(block)和多gpu设备同步数据概念
c++·人工智能·计算机视觉·数据挖掘·cuda编程·并行化计算·并行化计算与cuda编程
理论最高的吻2 小时前
HJ33 整数与IP地址间的转换【牛客网】
c++·算法·牛客网·ip地址转换
我漫长的孤独流浪2 小时前
STL中的Vector(顺序表)
开发语言·c++·算法
编程版小新2 小时前
封装红黑树实现mymap和myset
c++·学习·set·map·红黑树·红黑树封装set和map·红黑树封装
whoarethenext2 小时前
c/c++的opencv腐蚀
c语言·c++·opencv·腐蚀
乌鸦9443 小时前
《STL--string的使用及其底层实现》
开发语言·c++·stl·string使用及其模拟实现
一只鱼^_4 小时前
力扣第450场周赛
数据结构·c++·算法·leetcode·近邻算法·广度优先·图搜索算法
刚入门的大一新生5 小时前
C++初阶-list的使用1
开发语言·c++