关联容器(五):unordered_map

来源:互联网 发布:平面图纸设计软件 编辑:程序博客网 时间:2024/05/21 10:35
介绍:
哈希map是一种关联容器,通过键值和映射值存储元素。允许根据键值快速检索各个元素。
在内部unordered_map的元素不以键值或映射的元素作任何特定的顺序排序,其存储位置取决于哈希值,unordered_map容器比map容器更快地通过键值访问他们的单个元素(hash表的思想,以空间换时间),类似下图:

(键,值)对是插入的数据;

索引即对应桶(Bucket);

哈希码对应哈希函数的算法(库中可通过hash_function获取哈希函数);


常用函数:
(1)    构造函数/赋值
operator= 使用另一个unordered_map的拷贝替换unorder

(2)    增加函数/删除函数
clear 清空unordered_map
erase 从unordered_map中移除指定位置或范围的元素
emplace 插入一个元素(不执行copy或move操作)到unorder
emplace_hint 插入一个元素(不执行copy或move操作)到unorder
insert 在unordered_map的指定位置插入一或多个元素

(3)    遍历函数/访问函数
at 返回unordered_map中指定map键对应的map值
begin 返回指向unordered_map中第一个元素的forward
end 返回指向unordered_map的结尾位置之后位置的迭代器
cbegin 返回指向unordered_map中第一个元素的const迭
cend 返回指向unordered_map的结尾位置之后位置的con
equal_range 返回匹配指定key的子序列
operator[] 在unordered_map中查找或插入一个元素

(4)查找/替换/比较
find 返回指向unordered_map中指定key的元素位置的迭
key_eq 返回创建unordered_map使用的comparison

(5)    判断函数
empty 如果unordered_map为空,返回true

(6)    大小函数/个数函数
count 返回unordered_map中指定键对应的元素个数
max_size 返回unordered_map的最大长度
size 返回unordered_map中的元素个数

(7)    其他函数
get_allocator 返回创建unordered_map的Allocator的拷贝
reserve 更改unordered_map对象的容量
swap 交换两个unordered_map中的元素

(8)哈希相关
bucket 返回指定mapkey的bucket索引
bucket_count 得到bucket的个数
bucket_size 返回bucket的大小(size)
hash_function 返回创建unordered_map使用的hashfuncti
load_factor 返回每个bucket中元素的平均个数
max_bucket_count 返回map中bucket的最大个数
max_load_factor 得到或设置每个bucket中元素的最大个数

rehash rehash当前unordered_map(当HashMap的容量达到threshold时就需要进行扩容,这个时候就要进行ReHash操作了,在扩容的过程中需要进行ReHash操作,而这是非常耗时的,在实际中应该尽量避免。)


示例代码:

#include "stdafx.h"#include <iostream>#include <map>#include <vector>#include <algorithm>#include <functional>//greater<Type>必须包含此文件#include <string>#include <unordered_map>using namespace std;int _tmain(int argc, _TCHAR* argv[]){vector<pair<string, int>> vPair;vPair.push_back(make_pair("bbbb", 2222));vPair.push_back(make_pair("aaaa", 3333));vPair.push_back(make_pair("aaaa", 565565));vPair.push_back(make_pair("cccc", 4444));vPair.push_back(make_pair("dddd", 5555));unordered_map<string, int> unordered_mapTest;unordered_mapTest.insert(vPair.begin(), vPair.end());//插入vector-pairunordered_map<string, int>::iterator iter;for (iter = unordered_mapTest.begin(); iter != unordered_mapTest.end(); iter++)cout << iter->first << ",bucket:" << unordered_mapTest.bucket(iter->first) << endl;cout << " mapTest[\"dddd\"]:" << unordered_mapTest["dddd"] << endl;cout << " mapTest.at(\"dddd\"):" << unordered_mapTest.at("dddd") << endl;cout << "mapTest.find(\"dddd\"):" << unordered_mapTest.find("dddd")->first << "," << unordered_mapTest.find("dddd")->second << endl;cout << unordered_mapTest.bucket("bbbb") << endl;unordered_map<string, int>::hasher hasherFunc= unordered_mapTest.hash_function();cout << "hasher code of (\"dddd\"):" << hasherFunc("aaaa") << endl;cout << "aaaa哈希表中对应桶为:" << unordered_mapTest.bucket("aaaa") << endl;cout << "bbbb哈希表中对应桶为:" << unordered_mapTest.bucket("bbbb") << endl;cout << "哈希表中桶的数量:" << unordered_mapTest.bucket_count()<< endl;unordered_map<string, int>::key_equal keyeq = unordered_mapTest.key_eq();cout << "key_equal:" << keyeq("aaaa","bbbb") << endl;return 0;}
输出:

dddd,bucket:5bbbb,bucket:5cccc,bucket:1aaaa,bucket:1 mapTest["dddd"]:5555 mapTest.at("dddd"):5555mapTest.find("dddd"):dddd,55555hasher code of ("dddd"):1290481081aaaa哈希表中对应桶为:1bbbb哈希表中对应桶为:5哈希表中桶的数量:8key_equal:0请按任意键继续. . .
常用函数参考连接:http://www.howsoftworks.net/cplusplus.api/std/indexunordered_map.html


0 0
原创粉丝点击