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

 

  代码1

 

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

 

  执行结果1

 

[cpp] view plaincopy
 
  1. A()  
  2. other  
  3. other  
  4. ~A()  
  5. ~A()  
  6. ~A()  

 

  代码2

 

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

 

  结果2

 

[cpp] view plaincopy
 
  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

 

[cpp] view plaincopy
 
  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

 

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

 

  结果

 

[cpp] view plaincopy
 
  1. A()  
  2. other  
  3. other  
  4. ----------  
  5. ~A()  
  6. ~A()  
  7. ~A()  

 

posted @ 2014-05-22 11:23  CPYER  阅读(1565)  评论(0编辑  收藏  举报