news 2026/5/19 13:19:51

C++中的unordered_map容器详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++中的unordered_map容器详解

C++中的unordered_map容器详解

1.unordered_map概述

unordered_map是C++11引入的关联容器,基于哈希表实现,存储键值对(key-value pairs),提供快速的查找、插入和删除操作,平均时间复杂度为O(1)O(1)O(1)

2. 基本特性

  • 哈希表实现:使用哈希函数组织键值对
  • 唯一键:每个键只能出现一次
  • 无序存储:元素不以任何特定顺序存储
  • 快速访问:平均情况下提供常数时间复杂度的查找
  • 动态大小:可以根据需要自动扩展

3. 头文件与声明

#include<unordered_map>usingnamespacestd;unordered_map<int,string>um1;// 空unordered_mapunordered_map<string,double>um2={{"pi",3.14},{"e",2.71}};// 初始化列表unordered_map<char,int>um3(10);// 初始桶数为10

4. 构造函数与初始化

4.1 默认构造

unordered_map<string,int>wordCount;

4.2 范围构造

pair<string,int>arr[]={{"apple",5},{"banana",3}};unordered_map<string,int>fruitCount(arr,arr+2);

4.3 拷贝构造

unordered_map<string,int>um2(um1);

4.4 自定义哈希函数和相等比较

structCaseInsensitiveHash{size_toperator()(conststring&s)const{size_t h=0;for(charc:s){h+=tolower(c);}returnh;}};structCaseInsensitiveEqual{booloperator()(conststring&a,conststring&b)const{if(a.length()!=b.length())returnfalse;for(size_t i=0;i<a.length();++i){if(tolower(a[i])!=tolower(b[i]))returnfalse;}returntrue;}};unordered_map<string,int,CaseInsensitiveHash,CaseInsensitiveEqual>case_insensitive_map;

5. 容量操作

5.1size()

cout<<um.size();// 返回键值对数量

5.2empty()

if(um.empty()){cout<<"unordered_map is empty";}

5.3max_size()

cout<<um.max_size();// 返回可容纳的最大键值对数

6. 元素访问

6.1operator[]

um["apple"]=5;// 插入或修改键"apple"对应的值intcount=um["apple"];// 访问键"apple"对应的值

6.2at()

try{intval=um.at("orange");// 访问键"orange"对应的值(边界检查)}catch(constout_of_range&e){cerr<<"Key not found: "<<e.what()<<endl;}

6.3 迭代器访问

for(autoit=um.begin();it!=um.end();++it){cout<<it->first<<": "<<it->second<<endl;}

7. 修改操作

7.1insert()

um.insert({"banana",3});// 插入单个键值对um.insert({{"pear",2},{"orange",4}});// 插入初始化列表autoresult=um.insert(make_pair("apple",6));// 返回pair<iterator, bool>

7.2emplace()

autoresult=um.emplace("grape",7);// 原地构造键值对

7.3erase()

um.erase("apple");// 删除键"apple"对应的键值对autoit=um.find("banana");if(it!=um.end()){um.erase(it);// 通过迭代器删除}um.erase(um.begin(),um.end());// 删除范围

7.4clear()

um.clear();// 清空所有键值对

7.5swap()

unordered_map<string,int>um2;um.swap(um2);// 交换两个unordered_map

8. 查找操作

8.1find()

autoit=um.find("apple");// 返回指向键"apple"的迭代器if(it!=um.end()){cout<<"Found: "<<it->second;}

8.2count()

cout<<um.count("banana");// 返回键"banana"的数量(0或1)

8.3equal_range()

autorange=um.equal_range("apple");// 返回等于"apple"的键值对范围

9. 桶操作

9.1bucket_count()

cout<<um.bucket_count();// 返回桶的数量

9.2 `max_bucket_count()

cout<<um.max_bucket_count();// 返回最大桶数

9.3bucket_size()

cout<<um.bucket_size(2);// 返回第2个桶中的键值对数

9.4bucket()

cout<<um.bucket("apple");// 返回"apple"所在的桶索引

10. 哈希策略

10.1load_factor()

cout<<um.load_factor();// 返回负载因子(键值对数/桶数)

10.2max_load_factor()

cout<<um.max_load_factor();// 返回最大负载因子um.max_load_factor(0.75);// 设置最大负载因子

10.3rehash()

um.rehash(20);// 设置桶数为至少20

10.4reserve()

um.reserve(100);// 预留空间至少容纳100个键值对

11. 完整示例

#include<iostream>#include<unordered_map>#include<string>usingnamespacestd;intmain(){// 创建并初始化unordered_mapunordered_map<string,int>wordCount={{"apple",5},{"banana",3},{"orange",2}};// 插入元素wordCount.insert({"grape",4});wordCount.emplace("pear",1);wordCount["kiwi"]=6;// 使用operator[]插入// 修改元素wordCount["apple"]+=2;// 查找元素if(wordCount.find("banana")!=wordCount.end()){cout<<"Banana count: "<<wordCount["banana"]<<endl;}// 遍历unordered_mapcout<<"All word counts:"<<endl;for(constauto&pair:wordCount){cout<<pair.first<<": "<<pair.second<<endl;}// 删除元素wordCount.erase("orange");autoit=wordCount.find("pear");if(it!=wordCount.end()){wordCount.erase(it);}// 桶信息cout<<"\nBucket information:"<<endl;cout<<"Number of buckets: "<<wordCount.bucket_count()<<endl;cout<<"Current load factor: "<<wordCount.load_factor()<<endl;// 调整哈希表wordCount.rehash(10);cout<<"After rehash, bucket count: "<<wordCount.bucket_count()<<endl;// 容量信息cout<<"\nSize: "<<wordCount.size()<<endl;cout<<"Is empty: "<<(wordCount.empty()?"Yes":"No")<<endl;return0;}

12. 性能提示

  1. 平均情况下查找、插入、删除时间复杂度为O(1)O(1)O(1)
  2. 最坏情况下(哈希冲突严重)时间复杂度退化为O(n)O(n)O(n)
  3. 负载因子过高会影响性能,可适时rehash()
  4. 自定义键类型需要提供哈希函数和相等比较
  5. 迭代器在插入操作后可能失效(重新哈希时)

13. 与map比较

特性unordered_mapmap
实现方式哈希表红黑树
元素顺序无序按键排序
查找复杂度平均O(1)O(1)O(1)O(log⁡2n)O(\log_2 n)O(log2n)
内存使用通常较少通常较多
迭代器稳定性插入可能失效稳定(除删除元素)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/7 3:10:41

VS Code插件优化与开发效率提升指南:AI功能扩展实用教程

VS Code插件优化与开发效率提升指南&#xff1a;AI功能扩展实用教程 【免费下载链接】cursor-free-vip [Support 0.45]&#xff08;Multi Language 多语言&#xff09;自动注册 Cursor Ai &#xff0c;自动重置机器ID &#xff0c; 免费升级使用Pro 功能: Youve reached your t…

作者头像 李华
网站建设 2026/5/3 14:48:55

消息被撤回看不到?这款工具让你轻松获取完整对话

消息被撤回看不到&#xff1f;这款工具让你轻松获取完整对话 【免费下载链接】RevokeMsgPatcher :trollface: A hex editor for WeChat/QQ/TIM - PC版微信/QQ/TIM防撤回补丁&#xff08;我已经看到了&#xff0c;撤回也没用了&#xff09; 项目地址: https://gitcode.com/Git…

作者头像 李华
网站建设 2026/5/16 0:45:18

多精度计算开源库实战指南:从零基础到高效应用

多精度计算开源库实战指南&#xff1a;从零基础到高效应用 【免费下载链接】mpir Multiple Precision Integers and Rationals 项目地址: https://gitcode.com/gh_mirrors/mp/mpir 在现代软件开发中&#xff0c;当你需要处理超过常规数据类型范围的数值时&#xff0c;多…

作者头像 李华
网站建设 2026/5/12 12:28:39

5个超实用技巧解决Axure本地化问题

5个超实用技巧解决Axure本地化问题 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包&#xff0c;不定期更新。支持 Axure 9、Axure 10。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 软件本地化是提升用户体验的关键…

作者头像 李华
网站建设 2026/5/15 12:25:28

MacOS OBS NDI插件配置完全指南:从安装到故障排除

MacOS OBS NDI插件配置完全指南&#xff1a;从安装到故障排除 【免费下载链接】obs-ndi NewTek NDI integration for OBS Studio 项目地址: https://gitcode.com/gh_mirrors/ob/obs-ndi 在MacOS系统上进行音视频制作时&#xff0c;MacOS OBS NDI插件配置是实现高质量视频…

作者头像 李华
网站建设 2026/5/9 2:57:13

跨平台文件访问工具:Ext2Read技术解析与应用指南

跨平台文件访问工具&#xff1a;Ext2Read技术解析与应用指南 【免费下载链接】ext2read A Windows Application to read and copy Ext2/Ext3/Ext4 (With LVM) Partitions from Windows. 项目地址: https://gitcode.com/gh_mirrors/ex/ext2read 在Windows与Linux双系统环…

作者头像 李华