代码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. swagger2使用日志

    ------------------------------------------------------------------------------------ 安装.配置.使用 参考:htt ...

  2. python函数基础学习

    函数的定义与调用: def 函数名(参数1,参数2): ‘’’函数注释’’’ print(‘函数体’) return 返回值 定  义:def关键字开关,空格之后接函数名和圆括号,最后冒号结尾 def ...

  3. RTT学习之线程

    一 线程的创建和删除:rt_thread_create()创建的句柄,对应的删除rt_thread_delete(),注意线程的删除只是将线程的状态该为close,进入空闲任务才删除.rt_threa ...

  4. Unity ContextMenu 上下文菜单

    新建脚本: public class ContextTesting : MonoBehaviour { [ContextMenu("哈哈")] void DoSomething() ...

  5. IO流等学习笔记

    1.为什么日期的开始是从1970年0101开始记录,计算机的日期记录是现在的时间距1970年的时间,可正可负.? 2.引用类型默认都为null,基本数据类型为0,除基本数据类型外所有的都为引用数据类型 ...

  6. Ubuntu瞎胡搞

    ubutnu的几个必不可少的软件: 1.Guake,超级方便的终端 2.Unity tweak tool 3.VLC,本地视频播放器 4.MyPint,简单画图软件 5.GIMP,PS工具 Subli ...

  7. GitKraken使用教程-基础部分(9)

    10.  合并分支并解决冲突(conflict) 1) 合并分支 在代码管理过程中,切换分支或者同步服务器代码时,常常会出现代码冲突的情况,这种情况出现的原因一般是由于两个分支对同一个文件进行修改, ...

  8. HDU 4512——吉哥系列故事——完美队形I——————【LCIS应用】

    吉哥系列故事——完美队形I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  9. linux程序分析工具介绍(一)—-”/proc”

    写在最前面:在开始本文之前,笔者认为先有必要介绍一下linux下的man,如果读者手头用linux系统,直接在终端输入man man便可以看到详细的说明,我在这里简单的总结一下,man命令是用来查看l ...

  10. C# 压缩 解压 复制文件夹,文件的操作

    命名空间:namespace System.IO.Compression 压缩: //目标文件夹 string fileDirPath = "/Downloads/试题" + us ...