当前位置: 首页 > 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则删除失败

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

相关文章:

  • tauri项目,如何在rust端读取电脑环境变量
  • 画质MxPro:优化手游体验,畅享高清画质
  • Linux初步介绍
  • 【VLNs篇】07:NavRL—在动态环境中学习安全飞行
  • 多轮对话实现
  • react更新页面数据,操作页面,双向数据绑定
  • 免费数学几何作图web平台
  • 在阿里云上搭建n8n
  • React Native 弹窗组件优化实战:解决 Modal 闪烁与动画卡顿问题
  • 【Mini-F5265-OB开发板试用测评】1、串口printf输出
  • C++中auto和auto
  • 芯片设计中的通信“动脉”:I2C与I3C IP深度解析
  • ubuntu清理垃圾
  • CTFshow-PWN-栈溢出(pwn48)
  • 【深度学习新浪潮】大模型中,active parameters和total parameters都是什么?
  • “扛不住了就排队!”——聊聊消息队列在高并发系统中的那些硬核用途
  • STM32使用旋转电位器自制调光灯
  • 麒麟系统编译安装QtCreator
  • 01__C++入门
  • 根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:
  • 从零手写Java版本的LSM Tree (五):布隆过滤器
  • CppCon 2015 学习:Transducers, from Clojure to C++
  • 蓝桥杯第十届国B 质数拆分
  • 深入解析 ReentrantLock:原理、公平锁与非公平锁的较量
  • DreamO字节开源图像编辑框架
  • IDC智能机房整体解决方案
  • 华为云Flexus+DeepSeek征文|基于华为云一键部署Dify平台,接入DeepSeek大模型,构建数据可视化助手应用实战指南
  • ubuntu22.04 安装docker 和docker-compose
  • windows系统MySQL安装文档
  • 【深度学习新浪潮】什么是credit assignment problem?