如何使用vector的reserve和resize方法

reserveresize是vector里两个很重要的方法,有效地使用这两个方法可以减少reallocate memory的次数,提高程序的性能,所以还是有必要去研究一下的,先来看一段简单的代码吧。

stdafx.h

[html]  view plain copy
  1. // stdafx.h : include file for standard system include files,  
  2. // or project specific include files that are used frequently, but  
  3. // are changed infrequently  
  4. //  
  5.   
  6. #pragma once  
  7.   
  8. #include "targetver.h"  
  9.   
  10. #include <stdio.h>  
  11. #include <tchar.h>  
  12.   
  13.   
  14.   
  15. // TODO: reference additional headers your program requires here  
  16. #include <iostream>  
  17. #include <vector>  
  18. #include <string>  
  19. using namespace std;  

 test.cpp

[cpp]  view plain copy
  1. // test.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. class A  
  7. {  
  8. public:  
  9.     A() : m_a(0)  
  10.     {  
  11.     }  
  12.   
  13.     A(int a) : m_a(a)  
  14.     {  
  15.     }  
  16.   
  17.     A(const A& a) : m_a(a.m_a)  
  18.     {  
  19.     }  
  20.   
  21.     ~A()  
  22.     {  
  23.     }  
  24.   
  25. private:  
  26.     int m_a;  
  27. };  
  28.   
  29. int _tmain(int argc, _TCHAR* argv[])  
  30. {  
  31.     vector<A> myVec;  
  32.     myVec.reserve(100);  
  33.     cout << "capacity:" << myVec.capacity() << endl;  
  34.     cout << "size:" << myVec.size() << endl;  
  35.   
  36.     for (int i = 0; i < 100; i++)  
  37.     {  
  38.         myVec.push_back(i);  
  39.     }  
  40.     cout << "capacity:" << myVec.capacity() << endl;  
  41.     cout << "size:" << myVec.size() << endl;  
  42.   
  43.     myVec.resize(102);  
  44.     myVec[100] = 1;  
  45.     myVec[101] = 2;  
  46.     cout << "capacity:" << myVec.capacity() << endl;  
  47.     cout << "size:" << myVec.size() << endl;  
  48.   
  49.     return 0;  
  50. }  

 输出:

输出

reserve用来(预留空间,)改变capacity,不改变size,会去分配内存,但不会构造出对象;如果改变后的capacity比当前capacity大,则capacity会变大;反之,capacity不变。可以用下面的代码去测试:

[cpp]  view plain copy
  1. vector<A> myVec;  
  2. myVec.reserve(100);  
  3. cout << "capacity:" << myVec.capacity() << endl;  
  4. cout << "size:" << myVec.size() << endl;  
  5.   
  6. myVec.reserve(90);  
  7. cout << "capacity:" << myVec.capacity() << endl;  
  8. cout << "size:" << myVec.size() << endl;  
  9.   
  10. myVec.reserve(110);  
  11. cout << "capacity:" << myVec.capacity() << endl;  
  12. cout << "size:" << myVec.size() << endl;  

输出:


resize用来改变vector的size,有可能也会改变capacity。如果改变后的size比当前capacity大,则capacity会变大,同时构造出多出来的对象;反之,capacity不变,同时析构一些不再需要的对象。可以用下面的代码去测试:

[cpp]  view plain copy
  1. vector<A> myVec;  
  2. myVec.resize(100);  
  3. cout << "capacity:" << myVec.capacity() << endl;  
  4. cout << "size:" << myVec.size() << endl;  
  5.   
  6. myVec.resize(90);  
  7. cout << "capacity:" << myVec.capacity() << endl;  
  8. cout << "size:" << myVec.size() << endl;  
  9.   
  10. myVec.resize(110);  
  11. cout << "capacity:" << myVec.capacity() << endl;  
  12. cout << "size:" << myVec.size() << endl;  

输出:


reserve和resize都不会使capacity变小,但都有可能使capacity变大,具体怎么变大,reserve和resize是不一样的,reserve能准确控制capacity;而resize不能,vc里是每次增大一半的当前capacity。可以用下面的代码去测试不用reserve和resize的情况(在这种情况下,每当capacity不够时,就会去allocate一块新的够大的内存,再释放以前的内存,效率是很低的):

[cpp]  view plain copy
  1. vector<A> myVec;  
  2. cout << "capacity:" << myVec.capacity() << endl;  
  3. cout << "size:" << myVec.size() << endl;  
  4.   
  5. for (int i = 0; i < 100; i++)  
  6. {  
  7.     myVec.push_back(i);  
  8.     cout << "capacity:" << myVec.capacity() << endl;  
  9.     cout << "size:" << myVec.size() << endl;  
  10. }  

输出:


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值