STL容器之<unordered_map>

测试环境

系统:ubuntu 22.04.2 LTS 64位
gcc版本:11.3.0
编辑器:vsCode 1.76.2

unordered_map介绍

  1. 关联式容器。
  2. 每个元素都包含用于一个键和对应的值。
  3. 元素是无序的。
  4. 键是唯一的。
  5. 支持单向迭代器。
  6. 插入元素时不会使迭代器失效,删除元素时会使指向已删除元素的迭代器失效。
  7. 插入、删除和查找元素时间复杂度为O(1),最坏情况下为O(n)。

头文件

#include <unordered_map>

模块类定义

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:表示所存储分配器的类型,负责元素内存的分配和释放。可选参数,一般不使用。

对象构造

/*构造一个空的map对象*/
std::unordered_map<int,int> unordermap1;

/*构造map对象,指定最小桶数量*/
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()键对应的值通过键访问值
[]键对应的值通过键访问值
/*使用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++)
{
    /* 不允许修改值,编译报错 */
    //*cItr += 10; 
    /* 访问元素 */
    std::cout <<"first:"<<cItr->first<< " second:"<<cItr->second<<std::endl;
}
std::cout<<std::endl;
/*正向随机访问迭代器,每个元素+10*/
std::unordered_map<int,int>::iterator itr;
for (itr = unordermapTest.begin(); itr != unordermapTest.end(); itr++)
{
    /* 修改元素值每个元素+10 */
    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}};
/*方式1, unordermapSwap1={{1,8},{2,8},{3,8},{4,8},{5,8}}, unordermapSwap2={{1,6},{2,6},{3,6},{4,6},{5,6}}*/
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;
}

/*unordermapSwap1={{1,6},{2,6},{3,6},{4,6},{5,6}}, unordermapSwap2={{1,8},{2,8},{3,8},{4,8},{5,8}}*/
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表

注:桶即指的哈希桶,详情自行查找学习哈希算法相关知识。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值