文章目录
-
- 测试环境
- unordered_map介绍
- 头文件
- 模块类定义
- 对象构造
- 初始化
- 元素访问
- 元素插入和删除
- 元素查找
- 容器大小
- 迭代器
- 元素交换
- 其他函数
测试环境
系统:ubuntu 22.04.2 LTS 64位
gcc版本:11.3.0
编辑器:vsCode 1.76.2
unordered_map介绍
- 关联式容器。
- 每个元素都包含用于一个键和对应的值。
- 元素是无序的。
- 键是唯一的。
- 支持单向迭代器。
- 插入元素时不会使迭代器失效,删除元素时会使指向已删除元素的迭代器失效。
- 插入、删除和查找元素时间复杂度为O(1),最坏情况下为O(n)。
头文件
#include
模块类定义
template<typename _Key,
typename _Tp,
typename _Hash = std::hash<_Key>,
typename _Pred = std::equal_to<_Key>,
typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >
class unordered_map{};
_Key:表示存储的键数据类型
_Tp:表示存储的值的数据类型
_Hash:哈希函数对象类型
_Pred:表示按照键的排序方式。
_Alloc:表示所存储分配器的类型,负责元素内存的分配和释放。可选参数,一般不使用。
对象构造
std::unordered_map<int,int> unordermap1;
std::unordered_map<int,int> unordermap2(10);
std::unordered_map<int,int> unordermap3(unordermap1);
std::unordered_map<int,int> unordermap4(std::move(unordermap1));
std::unordered_map<int,int> unordermap5({{6,1},{1,1},{2,2},{3,3},{4,4},{5,5}});
std::unordered_map<int,int> unordermap6(++unordermap5.begin(), unordermap5.end());
初始化
unordermap1 = {{6,10},{7,11},{8,12},{3,17},{4,18},{5,19}};
元素访问
函数名 |
返回值 |
功能 |
at() |
键对应的值 |
通过键访问值 |
[] |
键对应的值 |
通过键访问值 |
unordermap1 = {{2,10},{1,11},{4,12},{3,13},{5,14},{10,15},{6,16},{8,17},{7,18},{9,19}};
std::cout << "use at(): " << std::endl;
for (int i = 1; i <= 10; i++)
{
std::cout<<"key:"<<i<<" value:"<<unordermap1.at(i)<<std::endl;
}
std::cout << "use []: "<<std::endl;
for (int i = 1; i <= 10; i++)
{
std::cout<<"key:"<<i<<" value:"<<unordermap1[i]<<std::endl;
}
元素插入和删除
函数 |
返回值 |
功能 |
clear() |
无 |
清空所有元素 |
erase() |
迭代器或删除元素数量 |
清除指定位置的一个元素、通过迭代器指定范围内的元素或通过键删除值 |
emplace() |
键值对 |
插入元素。插入成功时第一个元素是指向新插入元素的迭代器,失败时第一个元素是指向以存在元素的迭代器,第二个元素表示插入结果,true成功,false失败。 |
emplace_hint() |
迭代器 |
插入成功返回新插入元素的迭代器,插入失败时返回已存在元素的迭代器 |
insert() |
键值对、迭代器、无 |
1)在指定位置插入1个元素。2)复制通过迭代器指定范围的元素。3)通过初始化列表插入元素。4)直接插入元素。 |
std::pair<std::unordered_map<int,int>::iterator,bool> pair1 = unordermap1.emplace(1,10);
std::cout << "emplace result is "<<std::boolalpha<<pair1.second<<std::endl;
std::cout << "pair1 iterator key is "<<pair1.first->first<<" pair1 iterator value is "<<pair1.first->second<<std::endl;
std::pair<int,int> pair2(11, 20);
std::pair<std::unordered_map<int,int>::iterator,bool> pair3 = unordermap1.emplace(std::move(pair2));
std::cout << "emplace result is "<<std::boolalpha<<pair3.second<<std::endl;
std::cout << "pair3 iterator key is "<<pair3.first->first<<" pair3 iterator value is "<<pair3.first->second<<std::endl;
std::unordered_map<int,int>::iterator itr1 = unordermap1.emplace_hint(++unordermap1.begin(),8,66);
std::cout << "itr1 key is "<<itr1->first<<" itr1 value is "<<itr1->second<<std::endl;
std::unordered_map<int,int>::iterator itr2 = unordermap1.erase(unordermap1.begin());
std::cout << "itr2 key is "<<itr2->first<<" itr2 value is "<<itr2->second<<std::endl;
std::unordered_map<int,int>::iterator itr3 = unordermap1.erase(unordermap1.begin(), ++unordermap1.begin());
std::cout << "itr3 key is "<<itr3->first<<" itr3 value is "<<itr3->second<<std::endl;
int iCount = unordermap1.erase(1);
std::cout << "erace element count is "<<iCount<<std::endl;
unordermap1.clear();
std::pair<std::unordered_map<int,int>::iterator,int> pair4 = unordermap1.insert({1,0});
std::cout << "insert result is "<<std::boolalpha<<pair4.second<<std::endl;
std::cout << "pair4 iterator key is "<<pair4.first->first<<" pair4 iterator value is "<<pair4.first->second<<std::endl;
std::pair<int,int> pair5(2,0);
std::pair<std::unordered_map<int,int>::iterator,int> pair6 = unordermap1.insert(std::move(pair5));
std::cout << "insert result is "<<std::boolalpha<<pair6.second<<std::endl;
std::cout << "pair6 iterator key is "<<pair6.first->first<<" pair6 iterator value is "<<pair6.first->second<<std::endl;
std::unordered_map<int,int>::iterator itr4 = unordermap1.insert(unordermap1.begin(),{3,3});
std::cout << "insert key is "<< itr4->first << ",insert value is "<<itr4->second<<std::endl;
std::pair<int,int> pair7(4,5);
std::unordered_map<int,int>::iterator itr5 = unordermap1.insert(unordermap1.begin(),std::move(pair7));
std::cout << "insert key is "<< itr5->first << ",insert value is "<<itr5->second<<std::endl;
unordermap1.insert({{5,2},{7,3},{6,4}});
std::unordered_map<int,int> unordermap8{{9,5},{10,6}};
unordermap1.insert(unordermap8.begin(),unordermap8.end());
元素查找
函数 |
返回值 |
功能 |
count() |
std::size_t |
返回给定键对应元素的数量 |
find() |
迭代器 |
查找指定键对应元素的位置,未找到则返回end() |
equal_range() |
键值对 |
查找与指定键匹配的范围 |
std::cout<<unordermap1.count(3)<<std::endl;
std::unordered_map<int,int>::iterator itr6 = unordermap1.find(4);
std::cout<<"find key is "<<itr6->first<<",find value is "<<itr6->second<<std::endl;
auto ret7 = unordermap1.equal_range(1);
if (unordermap1.end() != ret7.first)
{
std::cout<<"first key is "<<ret7.first->first<<",first value is "<<ret7.first->second<<std::endl;
}
if (unordermap1.end() != ret7.second)
{
std::cout<<"second key is "<<ret7.second->first<<",second value is "<<ret7.second->second<<std::endl;
}
容器大小
函数 |
返回值 |
功能 |
size() |
std::size_t |
获取当前容器中的元素数量 |
empty() |
bool |
判断当前容器是否为空,为空返回true,否则返回false |
max_size() |
std::size_t |
返回容器的最大容量 |
unordermap1 = {{1,2},{2,1},{5,3},{4,5},{3,3}};
std::cout<<unordermap1.size()<<std::endl;
std::cout<<unordermap1.max_size()<<std::endl;
std::cout<<std::boolalpha<<unordermap1.empty()<<std::endl;
迭代器
类型 |
功能 |
iterator |
正向访问迭代器。从前向后访问元素,可以读取也可以修改 |
const_iterator |
常量正向访问迭代器。从前向后访问元素,只能读取不能修改 |
函数 |
返回值 |
功能 |
begin() |
正向访问迭代器 |
返回指向unorder_map对象首元素所在位置的迭代器 |
end() |
正向访问迭代器 |
返回指向unorder_map对象末尾元素的下一个位置的迭代器 |
cbegin() |
常量正向访问迭代器 |
返回指向unorder_map对象首元素所在位置的常量迭代器 |
cend() |
常量正向访问迭代器 |
返回指向unorder_map对象末尾元素的下一个位置的迭代器 |
std::unordered_map<int,int> unordermapTest({{6,10},{7,11},{8,12},{9,13},{10,14},{1,15},{2,16},{3,17},{4,18},{5,19}});
std::unordered_map<int,int>::const_iterator cItr;
for (cItr = unordermapTest.cbegin(); cItr != unordermapTest.cend(); cItr++)
{
std::cout <<"first:"<<cItr->first<< " second:"<<cItr->second<<std::endl;
}
std::cout<<std::endl;
std::unordered_map<int,int>::iterator itr;
for (itr = unordermapTest.begin(); itr != unordermapTest.end(); itr++)
{
itr->second += 10;
std::cout <<"first:"<<itr->first<< " second:"<<itr->second<<std::endl;
}
std::cout<<std::endl;
元素交换
函数名 |
返回值 |
功能 |
swap() |
无 |
交换两个容器的元素 |
std::unordered_map<int,int> unordermapSwap1 = {{1,6},{2,6},{3,6},{4,6},{5,6}};
std::unordered_map<int,int> unordermapSwap2 = {{1,8},{2,8},{3,8},{4,8},{5,8}};
unordermapSwap1.swap(unordermapSwap2);
std::cout << "unordermapSwap1: "<<std::endl;
for (int i = 1; i<=5;i++)
{
std::cout<<"key:"<<i<<" value:"<<unordermapSwap1.at(1)<<std::endl;
}
std::cout << "unordermapSwap2: "<<std::endl;
for (int i = 1; i<=5;i++)
{
std::cout<<"key:"<<i<<" value:"<<unordermapSwap2.at(1)<<std::endl;
}
std::swap(unordermapSwap1,unordermapSwap2);
std::cout << "unordermapSwap1: "<<std::endl;
for (int i = 1; i<=5;i++)
{
std::cout<<"key:"<<i<<" value:"<<unordermapSwap1.at(1)<<std::endl;
}
std::cout << "unordermapSwap2: "<<std::endl;
for (int i = 1; i<=5;i++)
{
std::cout<<"key:"<<i<<" value:"<<unordermapSwap2.at(1)<<std::endl;
}
其他函数
函数名 |
返回值 |
功能 |
bucket() |
std::size_type |
获取给定键对应的存储桶的编号 |
bucket_count() |
std::size_type |
获取hash表的存储桶的总数量 |
bucket_size() |
std::size_type |
根据存储桶的编号获取存储桶的大小 |
load_factor() |
float |
获取每个存储桶的平均元素数量 |
max_bucket_count() |
std::size_type |
获取当前允许的最大存储桶数量 |
max_load_factor() |
float或无 |
获取或者设置每个存储桶的最大元素数量 |
rehash() |
无 |
根据指定的存储桶数量重新生成hash表 |
注:桶即指的哈希桶,详情自行查找学习哈希算法相关知识。