您的位置:首页 > 财经 > 产业 > 网页升级访问每天_优化推广的页面对于优化点击率起非常大的作用_深圳关键词自动排名_建设网站

网页升级访问每天_优化推广的页面对于优化点击率起非常大的作用_深圳关键词自动排名_建设网站

2025/4/22 19:42:31 来源:https://blog.csdn.net/weixin_52173250/article/details/147125905  浏览:    关键词:网页升级访问每天_优化推广的页面对于优化点击率起非常大的作用_深圳关键词自动排名_建设网站
网页升级访问每天_优化推广的页面对于优化点击率起非常大的作用_深圳关键词自动排名_建设网站

一、unordered_map

  • unordered_map 是 C++ STL 中的一个关联容器,它有如下特点
  1. unordered_map 存储键值对,使用哈希表实现

  2. unordered_map 的每个键在容器中只能出现一次

  3. unordered_map 的存储的键值对是无序的

  4. 平均情况下,查找、插入、删除都是 O(1) 时间复杂度


二、声明与初始化

#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{// 空 unordered_mapunordered_map<string, int> map1;// 初始化 unordered_mapunordered_map<string, int> map2 = {{"Alice", 25},{"Bob", 30} };return 0;
}

三、插入元素

#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{unordered_map<string, int> wordCount;// 使用 insert 方法wordCount.insert({ "apple", 3 });// 使用 operator[]wordCount["banana"] = 2;// 使用 emplace 方法wordCount.emplace("cherry", 5);return 0;
}

四、访问元素

1、演示
#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{unordered_map<string, int> map = {{"Alice", 25},{"Bob", 30} };// 使用 operator[]cout << map["Alice"] << endl;cout << map["Bob"] << endl;// 使用 at 方法try {int result = map.at("Tom");}catch (out_of_range& e) {cout << "Key not found" << endl;}return 0;
}
# 输出结果25
30
Key not found
2、注意事项
  • 使用 operator[] 访问,如果元素不存在,会创建新元素
#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{unordered_map<string, int> map = {{"Alice", 25},{"Bob", 30} };cout << "unordered_map 大小为:" << map.size() << endl;cout << map["Tom"] << endl;cout << "unordered_map 大小为:" << map.size() << endl;return 0;
}
# 输出结果unordered_map 大小为:2
0
unordered_map 大小为:3

五、遍历元素

#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{unordered_map<string, int> map = {{"Alice", 25},{"Bob", 30} };// 使用迭代器for (auto it = map.begin(); it != map.end(); it++) {cout << it->first << ": " << it->second << endl;}cout << "----------" << endl;// 使用范围 for 循环for (auto& it : map) {cout << it.first << ": " << it.second << endl;}return 0;
}
# 输出结果Alice: 25
Bob: 30
----------
Alice: 25
Bob: 30

六、删除元素

#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{unordered_map<string, int> map = {{"Alice", 25},{"Bob", 30},{"Tom", 35} };cout << "unordered_map 大小为:" << map.size() << endl;// 通过键删除map.erase("Bob");cout << "erase 操作之后,unordered_map 大小为:" << map.size() << endl;// 全部删除map.clear();cout << "clear 操作之后,unordered_map 大小为:" << map.size() << endl;return 0;
}
# 输出结果unordered_map 大小为:3
erase 操作之后,unordered_map 大小为:2
clear 操作之后,unordered_map 大小为:0

七、查找元素

#include <iostream>
#include <unordered_map>
#include <string>using namespace std;int main()
{unordered_map<string, int> map = {{"Alice", 25},{"Bob", 30} };auto it1 = map.find("Alice");if (it1 != map.end()){cout << it1->second << endl;}else {cout << "Key not found" << endl;}auto it2 = map.find("Tom");if (it2 != map.end()){cout << it2->second << endl;}else {cout << "Key not found" << endl;}return 0;
}
# 输出结果25
Key not found

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com