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 ...
随机推荐
- 18-----BBS论坛
BBS论坛(十八) 18.首页轮播图实现 (1)front/css/front_base.css .main-container{ width: 990px; margin: 0 auto; over ...
- spring中注解注入 context:component-scan 的使用说明
通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...
- 单片机的图形UI
https://www.st.com/content/st_com/en/stm32-graphic-user-interface.html TouchGFX Designer:如今免费,资源占用10 ...
- CAD安装失败怎样卸载CAD 2013?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- 3d Max 2015安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- InnoDB还是MyISAM?
两种类型最主要的差别就是Innodb 支持事务处理与外键和行级锁.而MyISAM不支持.所以MyISAM往往就容易被人认为只适合在小项目中使用. 我作为使用MySQL的用户角度出发,Innodb和My ...
- 登录mysql数据库出现 : ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) ER或者忘记密码
1. 在安装mysql的文件目录中找到配置文件my.ini ,然后右击用记事本打开 2. 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tabl ...
- 零基础逆向工程36_Win32_10_互斥体_互斥体与临界区的区别
1 引言 讲了第二个内核对象,互斥体.前面已经学过一个内核对象,线程.这节讲两个函数,WaitForSingleObject()和WaitForMultipleObjects().因此这两个函数是根据 ...
- 《SQLServer删除重复数据的方法》
方法一: declare @max integer,@id integer open cur_rows fetch cur_rows into @id,@max begin set rowcount ...
- CommonJS 的实现原理
CommonJS 使用 Node.js 的四个环境变量moduleexportsrequireglobal 只要能够提供这四个变量,浏览器就能加载 CommonJS 模块. Browserify 是目 ...