当前位置: 首页 > 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和引用语法来简化代码。需要注意其无序性和迭代器失效问题,根据场景选择合适的遍历方式。

相关文章:

  • 依存句法分析
  • 【群体结构ADMIXTURE之三】监督分群在祖先成分分析中的应用及原理
  • 剑指offer22_合并两个排序的链表
  • 386.字典序排序
  • Xshell 8 客户端 免费版无需注册
  • 内核性能调优
  • Java八股文——Spring「SpringCloud 篇」
  • 代码随想录算法训练营第60期第六十五天打卡
  • C++默认构造函数被隐式删除
  • 某区域汽车多家4S店销售数据重叠度分析
  • 汇编(函数调用)
  • Node.js版本管理
  • LangChain文档加载器自动选择器:支持多种文件格式的统一加载方法
  • SpreadJS 在预算编制中的应用深度剖析
  • 日本滨松R669光电倍增管Hamamatsu直径51 mm 直径端窗型扩展红多碱光阴极面光谱灵敏度特性:300 至 900 nm
  • 生鲜商城源码系统设计+springboot3+vue3+JDK17(带用户协同过滤个性化推荐算法)
  • AX620Q上模型部署流程
  • Android Compose 自定义滑动进度条
  • JAVA CAS 详解
  • 【学习笔记】RTSP-Ovnif-GB28181
  • 域名到期网站/网站seo查询
  • 怎么用腾讯云服务器做网站/今日国内新闻最新消息10条
  • 专业的网站建设方案/郑州网站关键词优化公司
  • 越秀区建网站的公司/上海优化网站seo公司
  • 做企业网站软件/2021年度关键词有哪些
  • 信阳 网站建设/seo托管公司