基于低层哈希表的key-value容器(map容器)

template<typename T,typename V>
struct LNode {
	T key;
	V value;
	LNode * next;
	LNode(const T m_key, const V m_value) :key(m_key), value(m_value), next(nullptr) {};
};

template<typename T, typename V>
class hash_map
{
public:
	void insert(T key, V value) {
		long hashValue = std::hash<T>()(key);//获取哈希值
		int hashKey = hashValue % HASH_NUM;//获取哈希索引
		LNode<T, V>* pNode = new LNode<T, V>(key, key);//构造插入的节点 K-V
		LNode<T, V>* LocalHash = HashTable[hashKey];//查看哈希表位置
		if (LocalHash == NULL) {//位置为空 直接插入
			HashTable[hashKey] = pNode;
		}
		else {//不为空 开链法
			LNode<T, V>* InsetHashLocal = nullptr;//记录最后一个链表的位置
			while (LocalHash != nullptr) {
				InsetHashLocal = LocalHash;
				LocalHash = LocalHash->next;//寻找链表最后位置
			}
			InsetHashLocal->next = pNode;//插入构造的节点
			LocalHash = pNode;//改变链表指针
		}
	}

	pair<T,V>find(T key) {
		long hashValue = std::hash<T>()(key);//获取哈希值
		int hashKey = hashValue % HASH_NUM;//获取哈希索引
		LNode<T, V>* node = HashTable[hashKey];//查看哈希表位置
		while (node != nullptr)//顺着链表寻找
		{
			if (node->key == key) {//找到对应的key
				cout << key << "," << node->value << endl;
				return { node->key,node->value };
			}
			node = node->next;
		}
		cout << "not find K-V data" << endl;
		return {-1,-1};
	}
	void remove(T key) {
		long hashValue = std::hash<T>()(key);//获取哈希值
		int hashKey = hashValue % HASH_NUM;//获取哈希索引
		LNode<T, V>* node = HashTable[hashKey];
		while (node != nullptr) {
			LNode<T, V>* temp = nullptr;
			if (node->key != key) {
				temp = node;
				node = node->next;
			}
			else{
				if (node->next) {
					temp->next = node->next;
				}
				else {
					temp->next = nullptr
				}
				break;
			}
			
		}
	}
	void print() {
		for (int i = 0; i < HASH_NUM; i++) {
			LNode<T, V>* node = HashTable[HASH_NUM];
			cout << i << ":";
			while (node != nullptr) {
				cout << "(" << node->key << "," << node->value << ")";
				node->next;
			}
			cout << endl;
		}
	}


private:
	LNode<T, V>*  HashTable[HASH_NUM];
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_mapC++ STL库中的一个哈希容器,它能够快速地根据key值查找对应的value,其底层结构是基于哈希表实现的。unordered_map可以用于存储键值对,其中key值用来索引value。由于哈希表的特性,在大多数情况下,unordered_map的查找速度都比其他容器如vector和map更快。 使用unordered_map时需要引入头文件<unordered_map>,并使用命名空间std。下面是一个unordered_map的使用示例: ```cpp #include <iostream> #include <unordered_map> using namespace std; int main() { // 定义并初始化unordered_map unordered_map<int, string> umap = { { 1, "One" }, { 2, "Two" }, { 3, "Three" } }; // 遍历unordered_map for (auto itr = umap.begin(); itr != umap.end(); itr++) { cout << "Key: " << itr->first << " Value: " << itr->second << endl; } // 查找某个key对应的value int key = 2; auto itr = umap.find(key); if (itr != umap.end()) { cout << "Value of key " << key << " is " << itr->second << endl; } // 删除某个key-value对 umap.erase(3); // 插入一个新的key-value对 umap.insert(make_pair(4, "Four")); // 修改某个key对应的value umap[2] = "New Two"; // 输出修改后的unordered_map for (auto itr = umap.begin(); itr != umap.end(); itr++) { cout << "Key: " << itr->first << " Value: " << itr->second << endl; } return 0; } ``` 上面的代码首先定义了一个类型为<int, string>的unordered_map,并初始化了三个key-value对。然后使用迭代器遍历了整个容器,并查找了key值为2的value。接着使用erase方法删除了key值为3的键值对。使用insert方法插入了一个新的键值对。最后使用数组形式修改了key值为2的value,并通过迭代器输出了修改后的容器。 总而言之,unordered_map是一个方便快速的哈希容器,具有高效的查找和插入操作。在实际使用中,可以根据不同的需求和特点选择不同的容器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值