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

【C++】unordered_set和unordered_map

unordered_set类

unordered_set类的介绍

1. 在使用unordered_set类时,必须包含 #include <unordered_set> 这一行。

2. unordered_set类的底层其实是一个哈希桶,使用时需要显示实例化

3. 下面是unordered_set类的官方文本介绍,里面有详细的用法讲解。

- C++ Reference https://legacy.cplusplus.com/reference/unordered_set/

unordered_set类对象的常见构造 

1. unordered_set<int> us1,什么也不需要传入,构造一个空的unordered_set类对象

2. unordered_set<int> us2(s1.begin(),s1.end()),使用另一个unordered_set类对象进行迭代器构造

3. unordered_set<int> us3(const unordered_set<int>& us2),使用另一个unordered_set类对象进行拷贝构造

#include <iostream>
#include <unordered_set>
using namespace std;int main()
{unordered_set<int> us1;unordered_set<int> us2(us1.begin(), us1.end());unordered_set<int> us3(us2);return 0;
}

unordered_set类对象的容量操作  

1. unordered_set.size(),返回unordered_set类对象有效元素个数

2. unordered_set.empty(),检测unordered_set类对象有效节点是否为空为空返回true不为空返回flase

#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{unordered_set<int> us1;us1.insert(1);us1.insert(3);us1.insert(2);us1.insert(4);us1.insert(6);us1.insert(5);cout << us1.size() << endl;//6cout << us1.empty() << endl;//0return 0;
}

unordered_set容器的修改操作 

1. unordered_set.insert(int num)向unordered_set类对象中插入整数num如果插入unordered_set类对象已有的元素则插入失败

2. unordered_set.erase(int num)向unordered_set类对象中删除整数num如果删除unordered_set类对象没有的元素则删除失败

3. unordered_set.erase(iterator pos)向unordered_set类对象中删除迭代器为pos的值

4. unordered_set.find(int num)检查unordered_set类对象中是否存在某个特定的元素num效率为log(N)如果找到了则返回指向元素num的迭代器如果没有找到则返回指向end()的迭代器

5. unordered_set.count(int num)检查unordered_set类对象中是否存在某个特定的元素num返回元素num的个数

#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{unordered_set<int> us1;us1.insert(1);us1.insert(3);us1.insert(2);us1.insert(4);us1.insert(6);us1.insert(5);us1.insert(5);us1.insert(5);for (auto e : us1){cout << e << " ";//1 3 2 4 6 5 }cout << endl;us1.erase(us1.begin());us1.erase(5);for (auto e : us1){cout << e << " ";//3 2 4 6}cout << endl;auto a = us1.find(4);if (a != us1.end()){cout << *a << endl;//4}return 0;
}

unordered_map类 

unordered_map类的介绍

1. 在使用unordered_map类时,必须包含 #include <unordered_map> 这一行。

2. unordered_map类的底层其实是一个哈希桶,使用时需要显示实例化

3. 下面是unordered_map类的官方文本介绍,里面有详细的用法讲解。

- C++ Reference https://legacy.cplusplus.com/reference/unordered_map/

unordered_map类对象的常见构造 

1. unordered_map<string,string> s1,什么也不需要传入,构造一个空的unordered_map类对象

2. unordered_map<string,string> s2(s1.begin(),s1.end()),使用另一个unordered_map类对象进行迭代器构造

3. unordered_map<string,string> s3(const unordered_mapmap<string,string>& s2),使用另一个unordered_map类对象进行拷贝构造

#include <iostream>
#include <unordered_map>
using namespace std;int main()
{unordered_map<string, int> s1;unordered_map<string, int> s2(s1.begin(), s1.end());unordered_map<string, int> s3(s2);return 0;
}

1. map类对象的初始化分为两种。 

2. 如果使用号,则为拷贝初始化;如果不使用号,则为直接初始化。 

#include <iostream>
#include <unordered_map>
using namespace std;int main()
{unordered_map<string, int> s1 = { {"苹果",1} ,{"梨子",1} };unordered_map<string, int> s2{ {"苹果",1} ,{"梨子",1} };return 0;
}

unordered_map类对象的修改操作 

1. unordered_map.insert({key,value})向unordered_map类对象中插入键值对如果插入unordered_map类对象已有的键则插入失败

2. unordered_map.find(key)检查unordered_map类对象中是否存在某个特定的键key效率为log(N)返回一个迭代器如果找到了则返回指向键key的迭代器如果没有找到则返回指向end()的迭代器

3. unordered_map.count(key)检查map类对象中是否存在某个特定的键key返回键key的个数

4. unordered_map.erase(key)向map类对象中删除键为key的键值对如果删除map类对象没有键key则删除失败

相关文章:

  • 生信服务器 | 做生信为什么推荐使用Linux服务器?
  • EC2安装WebRTC sdk-c环境、构建、编译
  • 字符串哈希+KMP
  • Android15默认授权浮窗权限
  • 【Android】Android 开发 ADB 常用指令
  • 项目部署到Linux上时遇到的错误(Redis,MySQL,无法正确连接,地址占用问题)
  • docker容器保存为不依赖基础镜像的独立镜像方法
  • Java中方法调用参数传递机制的理解和示例验证
  • 书籍“之“字形打印矩阵(8)0609
  • 【11408学习记录】考研数学攻坚:行列式本质、性质与计算全突破
  • 【大厂机试题解法笔记】矩阵匹配
  • MySQL JOIN 表过多的优化思路
  • Matlab | 基于matlab的图像去噪的原理及实现
  • ZeenWoman 公司数据结构文档
  • LeetCode - 260. 只出现一次的数字 III
  • 【学习记录】使用 Kali Linux 与 Hashcat 进行 WiFi 安全分析:合法的安全测试指南
  • R语言速释制剂QBD解决方案之二
  • R语言速释制剂QBD解决方案之一
  • 【安全篇】金刚不坏之身:整合 Spring Security + JWT 实现无状态认证与授权
  • 数据结构第5章:树和二叉树完全指南(自整理详细图文笔记)
  • 外贸网站怎么推广/站长工具果冻传媒
  • 东莞网站建设案例/建站推广
  • 郑州做网站要/免费网站制作
  • 上海电子网站建设/网络整合营销4i原则是指
  • 重庆奉节网站建设/腾讯企点官网
  • 交友网站模板下载/阿里云云服务平台