当前位置:网站首页>用vector保存对象时保存指针的优点, 以及reserve的使用
用vector保存对象时保存指针的优点, 以及reserve的使用
2022-06-12 12:06:00 【就想叫yoko】
代码1
执行结果1
代码2
结果2
把代码2注释掉的vec.reserve(3)打开, 结果3
说明在使用vector时, 插入的是要插入的对象的拷贝, 如果vector中的类对象比较大时, 会影响性能, 还有使用拷贝构造时的一些深浅拷贝的问题, 另外通过结果2与结果3的比较我们可以知道当vector开始申请的空间不够使用时, 它会再次申请空间并可能放弃原来申请的空间, 这样调用的拷贝构造次数就更多了, 所以我们在使用vector前应该通过它的成员函数reserve事先申请一个我们估计的值, 你可以放心, 当reserve的空间不够大时, vector仍然会自动申请空间
下面是使用vector中存放类指针的做法, 一定要注意插入vector中对象指针指向内容的生存周期问题, 另外如果是new出来的, 如果其他地方没有delete应该在适当的时候通过遍历vector查找出来进行delete
结果
边栏推荐
- object.defineProperty 基本使用
- Find the median of two ordered arrays (leetcode 4)
- Google Earth engine (GEE) - quick land classification by kmeans clustering (double for loop quick parameter adjustment)
- Data processing instructions of arm instruction set
- 邻居子系统之ARP协议数据处理过程
- LeetCode 1037. Effective boomerang (vector cross product)
- How to operate the newly revised Taobao merchants and what should be paid attention to
- The second regular match is inconsistent with the first one, and the match in the regular loop is invalid
- Automatic generation of folder directory structure
- 【Leetcode】416. Split equal sum subset
猜你喜欢

Who moved my package lock

【Leetcode】79. Word search

Left and right cases + rotating pictures of small dots + no time

PIP install in the CONDA environment cannot be installed into the specified CONDA environment (the default PIP installation location of the CONDA environment)

7-5 complex quaternion operation

TinyMCE realizes automatic uploading of pasted pictures

Open source project - (erp+ Hotel + e-commerce) background management system

A. Prefix range

【Leetcode】221. Largest Square

Dom+js+ carousel map + no time
随机推荐
树的前序,中序,后序遍历
Ficusjs series (I) introduction to ficusjs
Reprint --win10 open the task manager to solve the blue screen problem
Pytoch notes
promise的理解已经利用promise实现图片的预加载(顺序加载)
In navigation, the solution of adding borders to affect the layout
Traditional DOM rendering?
The second day of QML study
bind、call、apply三者的区别,还有bind()的封装
ARM处理器模式与寄存器
Data processing instructions of arm instruction set
机器学习基础概念
object. Defineproperty basic usage
mysql复习
Who moved my package lock
QML学习 第一天
DOM+JS+轮播图+无时间
PIP install in the CONDA environment cannot be installed into the specified CONDA environment (the default PIP installation location of the CONDA environment)
ELK搭建指南
【QNX Hypervisor 2.2 用户手册】4 构建QNX Hypervisor系统