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 ...
随机推荐
- mapreduce求平均数
1. 现有某电商关于商品点击情况的数据文件,表名为goods_click,包含两个字段(商品分类,商品点击次数),分隔符“ ”,由于数据很大,所以为了方便统计我们只截取它的一部分数据,内容如下 ...
- 转 ogg issue
1.http://www.dbdream.com.cn/2013/05/17/ogg-00446%E9%94%99%E8%AF%AF%E8%A7%A3%E5%86%B3/ OGG-00446错误解决 ...
- mac U盘安装盘制作
以下面的版本为例: Install_macOS_Sierra_10.12.6.dmg 解压到桌面,然后打命令 sudo /Users/love/Desktop/Install\ macOS\ Sier ...
- `aclocal-1.10' is missing on your system
root@ubuntu31:~/linux-ftools-master# makecd . && /bin/bash /root/linux-ftools-master/missing ...
- Fixed Function Shader
Fixed function shader(固定管线着色器) Shader "Custom/Text01" { //shader名称 ...
- (转)shell脚本输出带颜色字体
shell脚本输出带颜色字体 原文:http://blog.csdn.net/andylauren/article/details/60873400 输出特效格式控制:\033[0m 关闭所有属性 ...
- 设置webstorm的file watch 监视scss文件
参考:http://blog.founddrama.net/2013/04/watching-compass-files-in-webstorm/ 上面红色划线部分. 特别注意arguments: 像 ...
- mysql时间戳
select unix_timestamp('2013-01-01 10:10:10'); , '%Y-%m-%d %H:%i:%S' ) date_format(date,'%Y-%m-%d') - ...
- 记一次无法登录 wine QQ
入Linux坑第X天,过了五一小长假,回来布置我的环境,本来不应该装一些不必要的东西分自己心,但还是装上,以便不时之需. 把输入法装好后,就安装了QQ,查过资料,都说wine_QQ国际版可以使用,于是 ...
- MyEclipse项目中的文件点击右键Team选项中没有提交到SVN中的选项是怎么回事
MyEclipse项目中的文件点击右键Team选项中没有提交到SVN中的选项是怎么回事 其实你已经可以百度到很多方法: 例如下面博客提供的 http://www.xuebuyuan.com/95285 ...