CPP-STL:用vector保存对象时保存指针的优点, 以及reserve的使用(转)
代码1
- #include <vector>
- #include <stdio.h>
- class A
- {
- public:
- A()
- {
- printf("A()/n");
- }
- ~A()
- {
- printf("~A()/n");
- }
- A(const A& other)
- {
- printf("other/n");
- }
- };
- int main()
- {
- A a;
- A b(a);
- A c = a;
- return 0;
- }
执行结果1
- A()
- other
- other
- ~A()
- ~A()
- ~A()
代码2
- #include <vector>
- #include <stdio.h>
- class A
- {
- public:
- A()
- {
- printf("A()/n");
- }
- ~A()
- {
- printf("~A()/n");
- }
- A(const A& other)
- {
- printf("other/n");
- }
- };
- int main()
- {
- A a;
- A b(a);
- A c = a;
- printf("----------/n");
- std::vector<A> vec;
- //vec.reserve(3);
- vec.push_back(a);
- vec.push_back(b);
- vec.push_back(c);
- return 0;
- }
结果2
- A()
- other
- other
- ----------
- other
- other
- ~A()
- other
- other
- other
- ~A()
- ~A()
- other
- other
- other
- other
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
把代码2注释掉的vec.reserve(3)打开, 结果3
- A()
- other
- other
- ----------
- other
- other
- other
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
- ~A()
说明在使用vector时, 插入的是要插入的对象的拷贝, 如果vector中的类对象比较大时, 会影响性能, 还有使用拷贝构造时的一些深浅拷贝的问题, 另外通过结果2与结果3的比较我们可以知道当vector开始申请的空间不够使用时, 它会再次申请空间并可能放弃原来申请的空间, 这样调用的拷贝构造次数就更多了, 所以我们在使用vector前应该通过它的成员函数reserve事先申请一个我们估计的值, 你可以放心, 当reserve的空间不够大时, vector仍然会自动申请空间
下面是使用vector中存放类指针的做法, 一定要注意插入vector中对象指针指向内容的生存周期问题, 另外如果是new出来的, 如果其他地方没有delete应该在适当的时候通过遍历vector查找出来进行delete
- #include <vector>
- #include <stdio.h>
- class A
- {
- public:
- A()
- {
- printf("A()/n");
- }
- ~A()
- {
- printf("~A()/n");
- }
- A(const A& other)
- {
- printf("other/n");
- }
- };
- int main()
- {
- A *a = new A;
- A *b = new A(*a);
- A *c = new A(*a);
- printf("----------/n");
- std::vector<A*> vec;
- vec.reserve(3);
- vec.push_back(a);
- vec.push_back(b);
- vec.push_back(c);
- std::vector<A*>::iterator iter = vec.begin();
- for (; iter!=vec.end(); ++iter)
- {
- delete *iter; //*iter = a , b, c
- }
- vec.clear();
- return 0;
- }
结果
- A()
- other
- other
- ----------
- ~A()
- ~A()
- ~A()
CPP-STL:用vector保存对象时保存指针的优点, 以及reserve的使用(转)的更多相关文章
- 转载:用vector保存对象时保存指针的优点, 以及reserve的使用
#include <vector> #include <stdio.h> class A { public: A() { printf("A()/n"); ...
- 保存对象时碰到的问题-列名 'Discriminator' 无效
今天保存对象时碰到问题: {"列名 'Discriminator' 无效.\r\n列名 'Discriminator' 无效."} 百度了一下,百度找到的一个解决: http:/ ...
- 关于SessionFactory的不同实现类分别通过getCurrentSession()方法 和 openSession() 方法获取的Session对象在保存对象时的一些区别
一.单向多对一关联关系 一).使用LocalSessionFactoryBean类,即在applicationContext中配置的 <!-- 配置SessionFactory 使用LocalS ...
- JSON(未完待续,等讲到对象时再加)
1 定义 JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言:JSON 使用 Jav ...
- STL中的函数对象实现负数的定义
// // main.cpp // STL中的函数对象 // // Created by mac on 2019/5/2. // Copyright © 2019年 mac. All rights r ...
- STL中list中push_back(对象)保存对象的内部实现
STL中list中push_back(对象)保存对象的内部实现 1. 在容器中,存放的是对象拷贝 #include<iostream> #include<list> using ...
- 程序启动读取和关闭时保存应用程序设置(QSettings)
保存应用程序设置(QSettings)1. QSettings 类 QSettings 提供保存应用程序当前设置的接口,可以方便地保存程序的状态,例如窗口大小和位置,选项的选中状态等等.在 Windo ...
- tomcat cluster session同步时保存map数据遇到的问题
Tomcat Cluster官网:https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html(tomcat7.0) 场景: tomcat1 ...
- 使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly错误
使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOn ...
随机推荐
- idea进行断点快捷键
快捷键 功能描述 F8 单步调试,不进入函数内部 F7 单步调试,进入函数内部 Shift+F7 选择要进入的函数 Shift+F8 跳出函数 Alt+F9 运行到断点 Alt+F8 执行表达式查看结 ...
- Collections练习之对字符串先折半,再取最长的一个
不多说,直接上干货! 代码需求 由 [aa, abcde, cba, cba, nbaa, zzz] 变成 max=abcde CollectionsDemo.java package zhouls. ...
- Java对于成对括号的提取
在工作的项目当中,经运营人员的反馈,发现提供服务的指定属性字段的值为空,导致搜索引擎无法正常搜索到正确的结果. 原始的字符串提取程序为: // 只取对应符号分割的第一部分name.split(&quo ...
- mysql java 通用AES加密
最近有个需求,需要对数据库某些字段加密,调研发现采用AES加密的方式较多,而且反向解密速度快,符合需求,于是采用:下面是遇到的问题及相关代码 首先第一个问题,AES的秘钥是16位,mysql的密码长度 ...
- SEO--HTML meta标签总结
<!DOCTYPE html> <!-- 使用html5 doctype,不区分大小写--> <html lang="zh-CN"> <h ...
- 使用Zxing生成一维码和二维码
首先引用zxing.dll 到项目中引用 using System; using System.Collections.Generic; using System.Drawing; using Sys ...
- 微信小程序全选多选效果
效果图: 代码: wxml <view class='hei_top'> <view class='hei_p'>共 <text>4</text> 场& ...
- sqlserverToXML
--raw模式 以属性的形式展示select * from goods for xml rawselect *from goods for xml raw('goods') --修改节点名称selec ...
- MSSql关闭自增列
在对已经建好表结构的表抽取数据的时候,突然报错,根据Error发现,不能显式插入有自增列的值. 于是搜索后,用 set IDENTITY_INSERT #Tmp onset IDENTITY_INSE ...
- 认识less和webstrom的less配置
认识less和webstrom的less配置 今天完成的事情: 首先第一件事情是,整理一下常用的颜色摄取 #F1F1F1 google的设置页面的body的背景颜色 #FFF 为google的内容块的 ...