当前位置: 首页 > news >正文

遍历 unordered_map

在C++中遍历unordered_map可以通过多种方式实现,下面详细介绍常用的遍历方法及其特点:

一、使用迭代器遍历(最常用)

unordered_map的迭代器是双向迭代器,可以通过begin()end()获取迭代器范围。

1. C++11前的传统写法
#include <unordered_map>
#include <string>
#include <iostream>int main() {std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};// 声明迭代器类型为unordered_map的迭代器std::unordered_map<int, std::string>::iterator it;for (it = umap.begin(); it != umap.end(); ++it) {std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;}return 0;
}
2. C++11及以后的auto简化写法
#include <unordered_map>
#include <string>
#include <iostream>int main() {std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};// 使用auto自动推导迭代器类型for (auto it = umap.begin(); it != umap.end(); ++it) {std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;}return 0;
}
3. 基于范围的for循环(C++11)
#include <unordered_map>
#include <string>
#include <iostream>int main() {std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};// 直接遍历元素,item是pair的引用for (auto& item : umap) {std::cout << "Key: " << item.first << ", Value: " << item.second << std::endl;}// 若不需要修改,建议用const引用for (const auto& item : umap) {std::cout << "Key: " << item.first << ", Value: " << item.second << std::endl;}return 0;
}

二、遍历键或值

1. 仅遍历键
#include <unordered_map>
#include <iostream>int main() {std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};for (const auto& key : umap) {std::cout << "Key: " << key.first << std::endl;}return 0;
}
2. 仅遍历值
#include <unordered_map>
#include <iostream>int main() {std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};for (const auto& value : umap) {std::cout << "Value: " << value.second << std::endl;}return 0;
}

三、使用迭代器遍历的注意事项

  1. 无序性unordered_map基于哈希表实现,遍历时元素顺序是无序的,与插入顺序无关。
  2. 迭代器失效
    • 添加或删除元素可能导致迭代器失效(除了删除当前迭代器指向的元素)。
    • 删除元素时,建议使用安全的迭代器操作:
      for (auto it = umap.begin(); it != umap.end();) {if (it->first == 2) {it = umap.erase(it);  // erase返回下一个迭代器} else {++it;}
      }
      
  3. const迭代器:若不需要修改元素,使用const_iterator
    std::unordered_map<int, std::string>::const_iterator cit;
    for (cit = umap.begin(); cit != umap.end(); ++cit) {// 仅可读,不可修改
    }
    

四、遍历性能考虑

  • 时间复杂度:遍历unordered_map的时间复杂度为 (O(n)),其中 (n) 是元素数量。
  • 空间复杂度:仅使用迭代器,为 (O(1))。
  • 与有序容器的区别map基于红黑树,遍历时按键的顺序排列;unordered_map遍历时顺序随机。

五、示例:统计字符串中字符出现次数

#include <unordered_map>
#include <string>
#include <iostream>int main() {std::string str = "hello world";std::unordered_map<char, int> count;// 统计字符频率for (char c : str) {count[c]++;}// 遍历输出结果std::cout << "字符频率统计:" << std::endl;for (const auto& pair : count) {std::cout << "字符 '" << pair.first << "': " << pair.second << " 次" << std::endl;}return 0;
}

总结

遍历unordered_map的核心是使用迭代器或基于范围的for循环,推荐使用C++11的auto和引用语法来简化代码。需要注意其无序性和迭代器失效问题,根据场景选择合适的遍历方式。

http://www.xdnf.cn/news/986509.html

相关文章:

  • GFS 分布式文件系统
  • UE_Event Any Damage和OnTake Any Damage
  • JAVA CAS 详解
  • Docker完整教程 - 从入门到SpringBoot实战
  • JSON5 模块的作用与区别
  • 图标异常问题
  • 【Linux】进程控制(下)---程序替换宝藏岛
  • 如何排查PHP-FPM进程CPU占用100%的间歇性问题 (2025)
  • Unity 服务器交互开发指南
  • 基于RocketMQ源码理解顺序写、刷盘机制与零拷贝
  • 海康对接摄像头
  • Chromium 136 编译指南 Windows篇:获取源代码(五)
  • 基于贝叶斯学习方法的块稀疏信号压缩感知算法
  • Spring核心框架完全指南 - 基础知识全解析
  • 关于界面存在AB测试后UI刷新空白的问题
  • 计算机网络 : 传输层协议UDP与TCP
  • 设计原则——KISS原则
  • 过拟合和欠拟合
  • RAG技术全解析:从概念到实践,构建高效语义检索系统——嵌入模型与向量数据库搭建指南
  • java每日精进 6.11【消息队列】
  • C++11的特性上
  • Cursor 编程实践 — 开发环境部署
  • 案例8 模型量化
  • 使用MyBatis-Plus实现数据权限功能
  • 【Unity3D优化】优化多语言字体包大小
  • swagger通过配置将enum自动添加到字段说明中
  • PHP如何检查一个字符串是否是email格式
  • 【微信小程序】| 在线咖啡点餐平台设计与实现
  • 华为云Flexus+DeepSeek征文 | 基于华为云ModelArts Studio打造AingDesk AI聊天助手
  • list类型