代码1

  1. #include <vector>
  2. #include <stdio.h>
  3. class A
  4. {
  5. public:
  6. A()
  7. {
  8. printf("A()/n");
  9. }
  10. ~A()
  11. {
  12. printf("~A()/n");
  13. }
  14. A(const A& other)
  15. {
  16. printf("other/n");
  17. }
  18. };
  19. int main()
  20. {
  21. A a;
  22. A b(a);
  23. A c = a;
  24. return 0;
  25. }

执行结果1

  1. A()
  2. other
  3. other
  4. ~A()
  5. ~A()
  6. ~A()

代码2

  1. #include <vector>
  2. #include <stdio.h>
  3. class A
  4. {
  5. public:
  6. A()
  7. {
  8. printf("A()/n");
  9. }
  10. ~A()
  11. {
  12. printf("~A()/n");
  13. }
  14. A(const A& other)
  15. {
  16. printf("other/n");
  17. }
  18. };
  19. int main()
  20. {
  21. A a;
  22. A b(a);
  23. A c = a;
  24. printf("----------/n");
  25. std::vector<A> vec;
  26. //vec.reserve(3);
  27. vec.push_back(a);
  28. vec.push_back(b);
  29. vec.push_back(c);
  30. return 0;
  31. }

结果2

  1. A()
  2. other
  3. other
  4. ----------
  5. other
  6. other
  7. ~A()
  8. other
  9. other
  10. other
  11. ~A()
  12. ~A()
  13. other
  14. other
  15. other
  16. other
  17. ~A()
  18. ~A()
  19. ~A()
  20. ~A()
  21. ~A()
  22. ~A()
  23. ~A()
  24. ~A()
  25. ~A()

把代码2注释掉的vec.reserve(3)打开, 结果3

  1. A()
  2. other
  3. other
  4. ----------
  5. other
  6. other
  7. other
  8. ~A()
  9. ~A()
  10. ~A()
  11. ~A()
  12. ~A()
  13. ~A()

说明在使用vector时, 插入的是要插入的对象的拷贝, 如果vector中的类对象比较大时, 会影响性能, 还有使用拷贝构造时的一些深浅拷贝的问题, 另外通过结果2与结果3的比较我们可以知道当vector开始申请的空间不够使用时, 它会再次申请空间并可能放弃原来申请的空间, 这样调用的拷贝构造次数就更多了, 所以我们在使用vector前应该通过它的成员函数reserve事先申请一个我们估计的值, 你可以放心, 当reserve的空间不够大时, vector仍然会自动申请空间

下面是使用vector中存放类指针的做法, 一定要注意插入vector中对象指针指向内容的生存周期问题, 另外如果是new出来的, 如果其他地方没有delete应该在适当的时候通过遍历vector查找出来进行delete

  1. #include <vector>
  2. #include <stdio.h>
  3. class A
  4. {
  5. public:
  6. A()
  7. {
  8. printf("A()/n");
  9. }
  10. ~A()
  11. {
  12. printf("~A()/n");
  13. }
  14. A(const A& other)
  15. {
  16. printf("other/n");
  17. }
  18. };
  19. int main()
  20. {
  21. A *a = new A;
  22. A *b = new A(*a);
  23. A *c = new A(*a);
  24. printf("----------/n");
  25. std::vector<A*> vec;
  26. vec.reserve(3);
  27. vec.push_back(a);
  28. vec.push_back(b);
  29. vec.push_back(c);
  30. std::vector<A*>::iterator iter = vec.begin();
  31. for (; iter!=vec.end(); ++iter)
  32. {
  33. delete *iter;   //*iter = a , b, c
  34. }
  35. vec.clear();
  36. return 0;
  37. }

结果

  1. A()
  2. other
  3. other
  4. ----------
  5. ~A()
  6. ~A()
  7. ~A()

CPP-STL:用vector保存对象时保存指针的优点, 以及reserve的使用(转)的更多相关文章

  1. 转载:用vector保存对象时保存指针的优点, 以及reserve的使用

    #include <vector> #include <stdio.h> class A { public: A() { printf("A()/n"); ...

  2. 保存对象时碰到的问题-列名 'Discriminator' 无效

    今天保存对象时碰到问题: {"列名 'Discriminator' 无效.\r\n列名 'Discriminator' 无效."}  百度了一下,百度找到的一个解决: http:/ ...

  3. 关于SessionFactory的不同实现类分别通过getCurrentSession()方法 和 openSession() 方法获取的Session对象在保存对象时的一些区别

    一.单向多对一关联关系 一).使用LocalSessionFactoryBean类,即在applicationContext中配置的 <!-- 配置SessionFactory 使用LocalS ...

  4. JSON(未完待续,等讲到对象时再加)

    1 定义 JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言:JSON 使用 Jav ...

  5. STL中的函数对象实现负数的定义

    // // main.cpp // STL中的函数对象 // // Created by mac on 2019/5/2. // Copyright © 2019年 mac. All rights r ...

  6. STL中list中push_back(对象)保存对象的内部实现

    STL中list中push_back(对象)保存对象的内部实现 1. 在容器中,存放的是对象拷贝 #include<iostream> #include<list> using ...

  7. 程序启动读取和关闭时保存应用程序设置(QSettings)

    保存应用程序设置(QSettings)1. QSettings 类 QSettings 提供保存应用程序当前设置的接口,可以方便地保存程序的状态,例如窗口大小和位置,选项的选中状态等等.在 Windo ...

  8. tomcat cluster session同步时保存map数据遇到的问题

    Tomcat Cluster官网:https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html(tomcat7.0) 场景: tomcat1 ...

  9. 使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly错误

    使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOn ...

随机推荐

  1. Mysql 查看表数据以及索引大小

    如果想查看 Mysql 数据库的总的数据量或者某个表的数据或者索引大小,可以使用系统库 information_schema 来查询,这个系统库中有一个 TABLES 表,这个表是用来记录数据库中表的 ...

  2. Nvelocity中格式化金钱和日期

    //格式化金钱(  9,999.00)function formatCurrency(num) {    num = num.toString().replace(/\$|\,/g, '');    ...

  3. java多线程(一)

    一.进程,线程,并发,并行 1.1 进程和线程的区别       进程是指:一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程.比如在Windows系统中,一个运 ...

  4. Android官方架构组件介绍之ViewModel(三)

    ViewModel 像Activity,Fragment这类应用组件都有自己的生命周期并且是被Android的Framework所管理的.Framework可能会根据用户的一些操作和设备的状态对Act ...

  5. maya2016安装失败如何卸载重装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. (转)linux应用之test命令详细解析

    linux应用之test命令详细解析 原文:https://www.cnblogs.com/tankblog/p/6160808.html test命令用法. 功能:检查文件和比较值 1)判断表达式 ...

  7. 模拟登陆并爬取Github

    因为崔前辈给出的代码运行有误,略作修改和简化了. 书上例题,不做介绍. import requests from lxml import etree class Login(object): def ...

  8. php高级教程

    PHP - 多维数组 多维数组指的是包含一个或多个数组的数组. 注释:数组的维度指示您需要选择元素的索引数. 对于二维数组,您需要两个索引来选取元素 对于三维数组,您需要三个索引来选取元素 PHP - ...

  9. mysql关闭严格模式

    通过配置文件修改: linux找my.cnf文件 window的修改办法是找my.ini sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  ub ...

  10. css 动画 和 响应式布局和兼容性

    14.动画 -moz-:火狐, -ms-:IE,-webkit-:谷歌,-o-:欧朋 transform旋转 rotate旋转 scale放大 translate偏移量 skew倾斜度 transfo ...