C++中的Hashmap

C++中的Hashmap

Hashmap 结构

HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长。

HashMap是非线程安全的,只是用于单线程环境下,但是Hash Table是线程安全的(https://www.cnblogs.com/williamjie/p/9099141.html)

哈希表(key,value) 是把key值通过一个固定的算法即哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间里。

而当使用哈希表进行查询的时候,就是再次使用哈希函数将key转换为对应的数组下标,并定位到该空间获取value,如此一来,就可以充分利用到数组的定位性能进行数据定位。

哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的情况下,用空间换时间的做法是值得的。

哈希冲突: 我们使用一个下标范围比较大的数组来存储元素。可以设计一个函数(哈希函数, 也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,于是用这个数组单元来存储这个元素;也可以简单的理解为,按照关键字为每一 个元素“分类”,然后将这个元素存储在相应“类”所对应的地方。但是,不能够保证每个元素的关键字与函数值是一一对应的,因此极有可能出现对于不同的元素,却计算出了相同的函数值,这样就产生了“冲突”,换句话说,就是把不同的元素分在了相同的“类”之中。后面我们将看到一种解决“冲突”的简便做法。 总的来说,“直接定址”与“解决冲突”是哈希表的两大特点。(https://www.jianshu.com/p/b468abd86f61)

Hashmap库(头文件)

C++中Hashmap的库为unordered_map#include <unordered_map>
如果C++的版本低于C++11,则会报错:[Error] ‘unordered_map’ was not declared in this scope
则需要将头文件改为
#include<tr1/unordered_map>
using namespace std::tr1;

Hashmap操作

建立Hashmap

unordered_map<int,int> Hashmap;

建立迭代器

unordered_map<int,int>::iterator it;

插入键值对

insert函数
Hashmap.insert(make_pair<int,int>(1,3));
Hashmap.insert(make_pair(1,3));
以上两种皆可
通过键添加
Hashmap[3]=1;

其他函数

it = Hashmap.begin() //指向哈希表的第一个容器
it = Hashmap.end() //指向哈希表的最后一个容器,实则超出了哈希表的范围,为空
Hashmap.size() //返回哈希表的大小
Hashmap.empty() //判断哈希表是否为空,返回值为true/false
Hashmap.clear() //清空哈希表

通过key值查找键值对
it = Hashmap.find(2) //查找key为2的键值对是否存在 ,若没找到则返回Hashmap.end()
if(Hashmap.find(2)!=Hashmap.end()) //判断找到了key为2的键值对

通过key值查找该key值下的键值对对数
Hashmap.count(1) //返回 1

swap交换两个Hashmap的键值对
Hashmap1.swap(Hashmap2);
swap(Hashmap1,Hashmap2);

Hashmap的遍历

//第一种
unordered_map<int, int> Hashmap;
for (auto p : Hashmap) {
	int front = p.first;   //key
    int end = p.second;   //value
}

//第二种
unordered_map<int, int> Hashmap;
for(auto it=Hashmap.begin();it!=Hashmap.end();it++)
{
    int front = it->first;   //key
    int end = it->second;   //value
}

//第三种
unordered_map<int,int> hash;
unordered_map<int,int>::iterator it;
it = hash.begin();
while(it != hash.end())
{
	int front= it->first;   //key
	int end = it->second;   //value
	it++;
}
  • 19
    点赞
  • 123
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C没有内置的HashMap数据结构,但可以使用一些技巧来实现类似的功能。一种常见的方法是使用哈希表(hash table)或关联数组(associative array)来模拟HashMap。 在C,你可以使用结构体和数组来创建自己的哈希表。下面是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 10 // 定义哈希节点结构体 typedef struct { char* key; int value; } HashNode; // 创建哈希表结构体 typedef struct { HashNode* nodes[SIZE]; } HashMap; // 哈希函数 int hashFunction(const char* key) { int sum = 0; for (int i = 0; i < strlen(key); i++) { sum += (int)key[i]; } return sum % SIZE; } // 初始化哈希表 void initHashMap(HashMap* hashMap) { for (int i = 0; i < SIZE; i++) { hashMap->nodes[i] = NULL; } } // 插入键值对到哈希表 void insert(HashMap* hashMap, const char* key, int value) { int index = hashFunction(key); HashNode* newNode = (HashNode*)malloc(sizeof(HashNode)); newNode->key = strdup(key); newNode->value = value; hashMap->nodes[index] = newNode; } // 根据键查找值 int get(HashMap* hashMap, const char* key) { int index = hashFunction(key); HashNode* node = hashMap->nodes[index]; if (node != NULL && strcmp(node->key, key) == 0) { return node->value; } return -1; // 找不到返回-1 } int main() { HashMap hashMap; initHashMap(&hashMap); insert(&hashMap, "apple", 5); insert(&hashMap, "banana", 3); insert(&hashMap, "orange", 7); printf("Value for 'apple': %d\n", get(&hashMap, "apple")); printf("Value for 'banana': %d\n", get(&hashMap, "banana")); printf("Value for 'orange': %d\n", get(&hashMap, "orange")); return 0; } ``` 这个示例使用了一个简单的哈希函数,将键转换为索引值,并使用该索引将节点存储在哈希表的相应位置。insert函数将给定的键值对插入哈希表,get函数根据键查找对应的值。 请注意,这只是一个简单的示例,实际的哈希表实现可能需要更多的功能和更复杂的哈希函数,以提高性能和处理冲突等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值