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

C++中常见的容器类使用方法举例(vector、deque、map、set)

cpp中常见的容器类有vector、list、deque、map、set、unordered_map和unordered_set。

下面将举例直接说明各个容器的使用方法。

文章目录

    • 综合示例
      • 1. vector:动态数组,支持随机访问
      • 2. list:双向链表,支持双向遍历和插入删除
      • 3. deque:双端队列,支持首尾插入删除和随机访问
      • 4. map:红黑树实现的关联数组,支持按键访问和遍历
      • 5. set:红黑树实现的集合,支持按值访问和遍历
      • 6. unordered_map:哈希表实现的关联数组,支持按键访问和遍历
      • 7. unordered_set:哈希表实现的集合,支持按值访问和遍历
    • 检索方法示例
      • 1. vector:根据下标检索
      • 2. deque:根据下标检索
      • 3. set:根据值检索
      • 4. map:根据值检索
      • 5. unordered_set:根据值检索
      • 6. unordered_map:根据值检索

综合示例

1. vector:动态数组,支持随机访问

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v;// 添加元素v.push_back(1);v.push_back(2);v.push_back(3);// 遍历元素for (auto it = v.begin(); it != v.end(); ++it){cout << *it << " ";}cout << endl;// 访问元素cout << v[0] << endl;cout << v.at(1) << endl;// 删除元素v.erase(v.begin() + 1);// 大小和容量cout << v.size() << endl;cout << v.capacity() << endl;return 0;
}

2. list:双向链表,支持双向遍历和插入删除

#include <iostream>
#include <list>using namespace std;int main()
{list<int> l;// 添加元素l.push_back(1);l.push_back(2);l.push_back(3);l.push_front(0);// 遍历元素for (auto it = l.begin(); it != l.end(); ++it){cout << *it << " ";}cout << endl;// 访问元素cout << l.front() << endl;cout << l.back() << endl;// 删除元素l.pop_front();// 大小cout << l.size() << endl;return 0;
}

3. deque:双端队列,支持首尾插入删除和随机访问

#include <iostream>
#include <deque>using namespace std;int main()
{deque<int> d;// 添加元素d.push_back(1);d.push_front(0);d.push_back(2);// 遍历元素for (auto it = d.begin(); it != d.end(); ++it){cout << *it << " ";}cout << endl;// 访问元素cout << d[0] << endl;cout << d.at(1) << endl;// 删除元素d.pop_front();// 大小cout << d.size() << endl;return 0;
}

4. map:红黑树实现的关联数组,支持按键访问和遍历

#include <iostream>
#include <map>using namespace std;int main()
{map<string, int> m;// 添加元素m["apple"] = 1;m["banana"] = 2;m.insert(make_pair("orange", 3));// 遍历元素for (auto it = m.begin(); it != m.end(); ++it){cout << it->first << " " << it->second << endl;}// 访问元素cout << m["apple"] << endl;// 删除元素m.erase("banana");// 大小cout << m.size() << endl;return 0;
}

5. set:红黑树实现的集合,支持按值访问和遍历

#include <iostream>
#include <set>using namespace std;int main()
{set<int> s;// 添加元素s.insert(1);s.insert(2);s.insert(3);// 遍历元素for (auto it = s.begin(); it != s.end(); ++it){cout << *it << " ";}cout << endl;// 访问元素auto it = s.find(2);if (it != s.end()){cout << *it << endl;}// 删除元素s.erase(3);// 大小cout << s.size() << endl;return 0;
}

6. unordered_map:哈希表实现的关联数组,支持按键访问和遍历

#include <iostream>
#include <unordered_map>using namespace std;int main()
{unordered_map<string, int> um;// 添加元素um["apple"] = 1;um["banana"] = 2;um.insert(make_pair("orange", 3));// 遍历元素for (auto it = um.begin(); it != um.end(); ++it){cout << it->first << " " << it->second << endl;}// 访问元素auto it = um.find("apple");if (it != um.end()){cout << it->second << endl;}// 删除元素um.erase("banana");// 大小cout << um.size() << endl;return 0;
}

7. unordered_set:哈希表实现的集合,支持按值访问和遍历

#include <iostream>
#include <unordered_set>using namespace std;int main()
{unordered_set<int> us;// 添加元素us.insert(1);us.insert(2);us.insert(3);// 遍历元素for (auto it = us.begin(); it != us.end(); ++it){cout << *it << " ";}cout << endl;// 访问元素auto it = us.find(2);if (it != us.end()){cout << *it << endl;}// 删除元素us.erase(3);// 大小cout << us.size() << endl;return 0;
}

检索方法示例

  • 根据下标检索的容器类有vectordeque
  • 根据值检索的容器类有setmapunordered_setunordered_map

(感觉主要靠容器.find()方法、容器.count()方法或者还可以用algorithm库里面的find)

1. vector:根据下标检索

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v = {1, 2, 3};// 访问元素cout << v[0] << endl;cout << v.at(1) << endl;// 判断元素是否在容器内if (v.size() > 0 && v[0] == 1){cout << "1 is in the vector." << endl;}return 0;
}

2. deque:根据下标检索

#include <iostream>
#include <deque>using namespace std;int main()
{deque<int> d = {1, 2, 3};// 访问元素cout << d[0] << endl;cout << d.at(1) << endl;// 判断元素是否在容器内if (d.size() > 0 && d[0] == 1){cout << "1 is in the deque." << endl;}return 0;
}

3. set:根据值检索

#include <iostream>
#include <set>using namespace std;int main()
{set<int> s = {1, 2, 3};// 查找元素auto it = s.find(2);if (it != s.end()){cout << *it << " is in the set." << endl;}// 判断元素是否在容器内if (s.count(1) > 0){cout << "1 is in the set." << endl;}return 0;
}

4. map:根据值检索

#include <iostream>
#include <map>using namespace std;int main()
{map<string, int> m = {{"apple", 1}, {"banana", 2}, {"orange", 3}};// 查找元素auto it = m.find("banana");if (it != m.end()){cout << it->second << " is in the map." << endl;}// 判断元素是否在容器内if (m.count("apple") > 0){cout << "apple is in the map." << endl;}return 0;
}

5. unordered_set:根据值检索

#include <iostream>
#include <unordered_set>using namespace std;int main()
{unordered_set<int> us = {1, 2, 3};// 查找元素auto it = us.find(2);if (it != us.end()){cout << *it << " is in the unordered_set." << endl;}// 判断元素是否在容器内if (us.count(1) > 0){cout << "1 is in the unordered_set." << endl;}return 0;
}

6. unordered_map:根据值检索

#include <iostream>
#include <unordered_map>using namespace std;int main()
{unordered_map<string, int> um = {{"apple", 1}, {"banana", 2}, {"orange", 3}};// 查找元素auto it = um.find("banana");if (it != um.end()){cout << it->second << " is in the unordered_map." << endl;}// 判断元素是否在容器内if (um.count("apple") > 0){cout << "apple is in the unordered_map." << endl;}return 0;
}

相关文章:

C++中常见的容器类使用方法举例(vector、deque、map、set)

cpp中常见的容器类有vector、list、deque、map、set、unordered_map和unordered_set。 下面将举例直接说明各个容器的使用方法。 文章目录综合示例1. vector&#xff1a;动态数组&#xff0c;支持随机访问2. list&#xff1a;双向链表&#xff0c;支持双向遍历和插入删除3. de…...

什么是强缓存和协商缓存

什么是缓存 浏览器缓存就是把一个已经请求过的Web资源&#xff08;如html页面&#xff0c;图片&#xff0c;js&#xff0c;数据等&#xff09;拷贝一份副本储存在浏览器中。缓存会根据进来的请求保存输出内容的副本。当下一个请求来到的时候&#xff0c;如果是相同的URL&#…...

算法刷题之堆

1. heapq 堆 Python 中只有最小堆&#xff1a; import heapqa [] heapq.heappush(a, 3) # 添加元素 heapq.heappush(a, 2) heapq.heappush(a, 1) while len(a): # 判断堆的长度print(heapq.heappop(a)) # 弹出堆顶元素# 将列表转换为最小堆 nums [2, 3, 1, 4, 5, 6] hea…...

javaweb导师选择系统

本文以JSP为开发技术&#xff0c;实现了一个导师选择系统。导师选择系统分为三大模块&#xff0c;包括管理员&#xff1a;学员信息管理、导师信息管理、导师选择管理、导师分布图、公告信息管理、系统管理&#xff0c;学生&#xff1a;个人资料管理、导师选择管理、导师分布图管…...

LeetCode150 逆波兰表达式求值

题目&#xff1a; 给你一个字符串数组 tokens &#xff0c;表示一个根据 逆波兰表示法 表示的算术表达式。请你计算该表达式。返回一个表示表达式值的整数。 注意&#xff1a; 有效的算符为 ‘’、‘-’、‘*’ 和 ‘/’ 。每个操作数&#xff08;运算对象&#xff09;都可以…...

【Node.js】项目开发实战(中)

开发用户的注册和登录接口步骤1&#xff0c;打开MySQL Workbench&#xff0c;打开自己的数据库进入创建用户信息表新建 ev_users表安装并配置mysql模块安装mysql模块新建db文件夹下index.js,导入并配置mysql模块安装bcryptjs对密码进行加密处理新建/router_handler/user.js中&a…...

记录一次 New Bing 英语陪练

记录一次 New Bing 英语陪练 Now I start to speak english to chat with you . Help me find the mistake in my word and help me improve my english I’m glad you want to practice your English with me. I can help you find the mistakes in your words and help you i…...

【Python】照片居然能变素描?不会画画但是咱会代码

文章目录前言一、准备二、下载预训练模型总结前言 Photo-Sketching 一个能将照片的轮廓识别出来并将其转化为“速写”型图像的开源模块。 比如&#xff0c;这只小狗&#xff1a; 经过模型的转化&#xff0c;会变成卡通版的小狗&#xff1a; 非常秀&#xff0c;这很人工智能…...

已解决正确配置git环境变量

已解决git没有配置环境变量&#xff0c;抛出异常ERROR: Cannot find command ‘git’- do you have ‘git’ installed and in your PATH?&#xff0c;附上正确配置git环境变量的教程 文章目录报错问题报错翻译报错原因解决方法《100天精通Python》专栏推荐白嫖80g Python全栈…...

【逐步剖C】-第十章-自定义类型之结构体、枚举、联合

一、结构体 前言&#xff1a;有关结构体的声明、定义、初始化以及结构体的传参等结构体的基本使用在文章【逐步剖C】-第六章-结构体初阶中已进行了详细的介绍&#xff0c;需要的朋友们可以看看。这里主要讲解的是有关结构体的内存问题。 1. 结构体的内存对齐 &#xff08;1&…...

Windows Server 2016 中文版、英文版下载 (updated Mar 2023)

Windows Server 2016 Version 1607&#xff0c;2023 年 3 月更新 请访问原文链接&#xff1a;https://sysin.org/blog/windows-server-2016/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org 本站将不定期发布官方原版风格月度更…...

Linux 4G 通信实验

目录4G 网络连接简介高新兴ME3630 4G 模块实验ME3630 4G 模块简介ME3630 4G 模块驱动修改ME3630 4G 模块ppp 联网测试前面我们学习了如何在Linux 中使用有线网络或者WIFI&#xff0c;但是使用有线网络或者WIFI 有 很多限制&#xff0c;因为要布线&#xff0c;即使是WIFI 你也得…...

华为OSPF技术详细介绍,保姆级,谁都能看懂(一)

目录 1、简介 2、OSPF基本原理 3、OSPF的特点 4、OSPF区域 5、路由器的类型 6、OSPF5种报文 7、后半部分内容 1、简介 OSPF&#xff08;Open Shortest Path First&#xff0c;开放最短路径优先&#xff09;是一个基于链路状态的内部网关协 议。目前针对IPv4协议使用的是OS…...

行人车辆检测与计数系统(Python+YOLOv5深度学习模型+清新界面)

摘要&#xff1a;行人车辆检测与计数系统用于交通路口行人及车辆检测计数&#xff0c;道路人流量、车流量智能监测&#xff0c;方便记录、显示、查看和保存检测结果。本文详细介绍行人车辆检测&#xff0c;在介绍算法原理的同时&#xff0c;给出Python的实现代码、PyQt的UI界面…...

SM3哈希算法的FPGA实现 I

SM3哈希算法的FPGA实现 I SM3哈希算法的FPGA实现 I一、什么是SM3哈希算法&#xff1f;二、SM3哈希算法的具体内容1、填充2、迭代与压缩3、计算拼凑值三、参考文档语言 &#xff1a;verilog 仿真工具&#xff1a; Modelsim EDA工具&#xff1a;quartus II 一、什么是SM3哈希算法…...

【数据结构与算法】线性表--数组

文章目录一、前言二、数组的概念三、数组的操作数组的插入数组的删除四、容器与数组五、问题&#xff1a;为何数组要从0开始编号&#xff0c;而不是1开始呢&#xff1f;六、总结一、前言 常见的数据结构如下图&#xff0c;本文主要讲解数据结构线性表--数组。 二、数组的概念 …...

剑指offer排序专题

剑指offer排序专题 jz3 数组中重复的数字描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的&#xff0c;但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如&#xff0c;如果输入长度为7的数组[…...

已解决Cannot open D:\Soft\Python36\Scripts\pip3-script.py

已解决Cannot open D:\Soft\Python36\Scripts\pip3-script.py 文章目录报错问题报错翻译报错原因解决方法1&#xff1a;easy_install 来安装pip解决方法2&#xff1a;本地安装pip《100天精通Python》专栏推荐白嫖80g Python全栈视频报错问题 粉丝群里面的一个小伙伴遇到问题…...

3 步走,快速上手 API 接口测试

开始 API 接口测试之前&#xff0c;我们需要弄清接口测试的含义&#xff1a; 接口测试就是根据接口清单&#xff0c;模拟客户端向服务端发送请求数据&#xff0c;并获取响应数据后&#xff0c;查看响应数据是否符合预期的过程。 整个过程可以分为三个步骤&#xff1a; 第一步&…...

爬虫-day1-正则表达式作业

利用正则表达式完成下面的操作: 一、不定项选择题 能够完全匹配字符串"(010)-62661617"和字符串"01062661617"的正则表达式包括&#xff08;ABD &#xff09; A. r"\(?\d{3}\)?-?\d{8}" B. r"[0-9()-]" C. r"[0-9(-)]*\d*&…...

[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?

&#x1f9e0; 智能合约中的数据是如何在区块链中保持一致的&#xff1f; 为什么所有区块链节点都能得出相同结果&#xff1f;合约调用这么复杂&#xff0c;状态真能保持一致吗&#xff1f;本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里&#xf…...

label-studio的使用教程(导入本地路径)

文章目录 1. 准备环境2. 脚本启动2.1 Windows2.2 Linux 3. 安装label-studio机器学习后端3.1 pip安装(推荐)3.2 GitHub仓库安装 4. 后端配置4.1 yolo环境4.2 引入后端模型4.3 修改脚本4.4 启动后端 5. 标注工程5.1 创建工程5.2 配置图片路径5.3 配置工程类型标签5.4 配置模型5.…...

JVM垃圾回收机制全解析

Java虚拟机&#xff08;JVM&#xff09;中的垃圾收集器&#xff08;Garbage Collector&#xff0c;简称GC&#xff09;是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象&#xff0c;从而释放内存空间&#xff0c;避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

selenium学习实战【Python爬虫】

selenium学习实战【Python爬虫】 文章目录 selenium学习实战【Python爬虫】一、声明二、学习目标三、安装依赖3.1 安装selenium库3.2 安装浏览器驱动3.2.1 查看Edge版本3.2.2 驱动安装 四、代码讲解4.1 配置浏览器4.2 加载更多4.3 寻找内容4.4 完整代码 五、报告文件爬取5.1 提…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计

随着大语言模型&#xff08;LLM&#xff09;参数规模的增长&#xff0c;推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长&#xff0c;而KV缓存的内存消耗可能高达数十GB&#xff08;例如Llama2-7B处理100K token时需50GB内存&a…...

USB Over IP专用硬件的5个特点

USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中&#xff0c;从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备&#xff08;如专用硬件设备&#xff09;&#xff0c;从而消除了直接物理连接的需要。USB over IP的…...

招商蛇口 | 执笔CID,启幕低密生活新境

作为中国城市生长的力量&#xff0c;招商蛇口以“美好生活承载者”为使命&#xff0c;深耕全球111座城市&#xff0c;以央企担当匠造时代理想人居。从深圳湾的开拓基因到西安高新CID的战略落子&#xff0c;招商蛇口始终与城市发展同频共振&#xff0c;以建筑诠释对土地与生活的…...

快刀集(1): 一刀斩断视频片头广告

一刀流&#xff1a;用一个简单脚本&#xff0c;秒杀视频片头广告&#xff0c;还你清爽观影体验。 1. 引子 作为一个爱生活、爱学习、爱收藏高清资源的老码农&#xff0c;平时写代码之余看看电影、补补片&#xff0c;是再正常不过的事。 电影嘛&#xff0c;要沉浸&#xff0c;…...

[ACTF2020 新生赛]Include 1(php://filter伪协议)

题目 做法 启动靶机&#xff0c;点进去 点进去 查看URL&#xff0c;有 ?fileflag.php说明存在文件包含&#xff0c;原理是php://filter 协议 当它与包含函数结合时&#xff0c;php://filter流会被当作php文件执行。 用php://filter加编码&#xff0c;能让PHP把文件内容…...