当前位置:网站首页>STL教程10-容器共性和使用场景
STL教程10-容器共性和使用场景
2022-07-03 10:45:00 【贪睡的蜗牛】
共性
- 除了 queue 与 stack这两个访问受限的特殊容器外,每个容器都提供可返回迭代器的函数,运用返回的迭代器就可以访问元素。
- 通常 STL 不会丢出异常。要求使用者确保传入正确的参数。
- 每个容器都提供了一个默认构造函数跟一个默认拷贝构造函数。
- 大小相关的构造方法:1 size()返回容器中元素的个数 2empty()判断容器是否为空
STL容器使用时机
1)vector使用场景
比如软件历史操作记录的存储,历史记录,上一次的记录
2)deque使用场景
要进行先进先出的场景,比如排队购票系统,对排队者的存储可以采用 deque,支持头端的快速移除,尾端的快速添加。如果采用 vector,则头端移除时,会移动大量的数据,速度慢。
vector和deque比较
- vector.at()比 deque.at()效率高,比如 vector.at(0)是固定的,deque 的开始位置却是不固定的。
- 如果有大量释放操作的话,vector 花的时间更少,这跟二者的内部实现有关。
- deque 支持头部的快速插入与快速移除,这是 deque 的优点。
3)list使用场景
比如公交车乘客的存储,随时可能有乘客下车,支持频繁的不确实位置元素的移除插入
4)set使用场景
比如对手机游戏的个人得分记录的存储,存储要求从高分到低分的顺序排列。
5)map使用场景
比如按 ID 号存储十万个用户,想要快速要通过 ID 查找对应的用户。二叉树的查找效率,这时就体现出来了。如果是 vector 容器,最坏的情况下可能要遍历完整个容器才能找到该用户。
边栏推荐
- This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
- Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
- Illustrated network: what is virtual router redundancy protocol VRRP?
- Oracle withdraw permission & create role
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- Excel快速跨表复制粘贴
- PHP server interacts with redis with a large number of close_ Wait analysis
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
- .\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
- Some common terms
猜你喜欢
活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
FL Studio 20无限试用版水果编曲下载
C language AES encryption and decryption
Excel表格转到Word中,表格不超边缘纸张范围
2022 东北四省赛 VP记录/补题
高精度室内定位技术,在智慧工厂安全管理的应用
The world's most popular font editor FontCreator tool
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
AMS Series 1 - AMS startup process
随机推荐
One hot code
Arctangent entropy: the latest SCI paper in July 2022
Web安全总结
Asyncio warning deprecationwarning: there is no current event loop
[OBS] encapsulate the basic process of OBS acquisition
FL Studio 20 unlimited trial fruit arranger Download
FL Studio 20无限试用版水果编曲下载
Struct function & function pointer
CSRF
高精度室内定位技术,在智慧工厂安全管理的应用
AMS series - application startup process
Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
[OBS] configFile in ini format of OBS
Excel表格转到Word中,表格不超边缘纸张范围
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
How to become a senior digital IC Design Engineer (1-5) Verilog coding syntax: operand
LeetCode 46:全排列
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~