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

网站建设技术风险外贸营销

网站建设技术风险,外贸营销,互联网前端开发,网上做任务赚钱的比较正规的网站在C中遍历unordered_map可以通过多种方式实现,下面详细介绍常用的遍历方法及其特点: 一、使用迭代器遍历(最常用) unordered_map的迭代器是双向迭代器,可以通过begin()和end()获取迭代器范围。 1. C11前的传统写法 …

在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.cadmedia.cn/news/9466.html

相关文章:

  • 语言教学网站建设课程总结佛山seo
  • 个人可以建立网站吗营销管理培训课程培训班
  • 温州网站设计定制网站关键字优化技巧
  • 国外网站建设素材库搜索引擎优化方法有哪几种
  • 网站建设及使用网站权重怎么看
  • 360免费wifi旧版本下载福州seo优化排名推广
  • 河南省级住房城乡建设主管部门网站线下引流推广方法
  • 语音识别程序代做网站职业技能培训学校
  • 汕头市网站建设个人怎么注册自己的网站
  • 网站开发都是用什么做的b2b多平台一键发布
  • 网站优化有前途吗怎样推广自己的店铺啊
  • 收录入口在线提交优化加速
  • 为什么访问外国网站速度慢怎么做小说推广挣钱
  • 企业网站维护工作计划贴吧推广400一个月
  • 天猫店怎么申请广西网络优化seo
  • 加强网站的建设工作的通知怎样进行关键词推广
  • 牛 网站建设网页界面设计
  • 福建建设动态网站百度推广开户费
  • 开发一个跑腿app需要多少钱南宁百度seo建议
  • 企业网络推广平台公司沈阳百度seo
  • 网络规划与设计师真实通过率seo搜索引擎营销工具
  • 水木网站建设徐州网页关键词优化
  • 企业建站seo公司资源
  • 职教集团网站建设怎么样建一个网站
  • 江苏省建设厅纪委网站免费打广告网站
  • 网站管理制度建设的必要性品牌营销是什么
  • 政府网站开发文档南宁seo结算
  • 装修设计效果图网站广告投放运营主要做什么
  • 百捷网站建设工资百度云登陆首页
  • 网站空间是指什么网站运营是做什么的