STL 中vector的reserve()和resize()

C++中vector的reserve()和resize()

1 reserve()

首先,从字面意思理解,reserve的意思是:预留。那么这个函数是如何控制空间的呢?

vector<int> vec(10);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

vec.reserve(5);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

vec.reserve(25);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

运行结果:

10
10
10
10
10
25

从执行结果可以知道(假设reserve的参数为n,vector的容量为c):

  • 当在创建vector时,指定大小参数可以将vector的大小和容量都设置为参数值。
  • 在调用reserve(),如果n <= c,什么也不干。
  • 在调用reserve(),如果n > c,则进行空间的再分配,使得vector可以容纳n个元素,但是原来的元素并没有改变 。

因此,reserve()的实现可能类似:

template < typename T >
void vector<T>::reserve(size_t n)
{
    if(n > c) {
        ptr = realloc(ptr, n * sizeof(T));
    }
}

注意:上面的代码中,为了方便,模板参数只写了一个。其中,ptr是内部使用的实际存储数据的指针。

2 resize()

下面再来看看resize():

第一个例子:

vector<int> vec(10);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

vec.resize(5);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

vec.resize(25);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

运行结果:

10
10
5
10
25
25

第二个例子:

vector<int> vec(10);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

vec.resize(7);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

vec.resize(11);
cout << vec.size() << endl;
cout << vec.capacity() << endl;

运行结果:

10
10
7
10
11
14

从执行结果可以知道(假设resize的参数为n,vector的元素个数为s,vector的容量为c):

  • 如果n <= c,vector的底层不会发生空间的再分配,而是根据n和s的大小关系删除或者添加元素(n < s时,会删除后面的s-n个元素;n > s时,会在vector后面添加n-s个元素)。
  • 如果n > c时,vector的底层会发生空间的再分配。如果2 * s >= n时,就重新分配2 * s * sizeo(T)的空间。如果2 * s < n时,就分配n * sizeo(T)的空间。

因此,resize()的实现可能类似:

template < typename T >
void vector<T>::resize(size_t n)
{
    if(n <= c) {
        if(n < s) {
            // delete s-n elements
        }
        else if(n > s) {
            // insert n-s elements
        }
    }
    else {
        if(2 * s >= n) {
            ptr = realloc(ptr, 2 * s * sizeof(T));
            // delete or insert elements to make size n
        }
        else {
            ptr = realloc(ptr, n * sizeof(T));
            // delete or insert elements to make size n
        }
    }
}

3 小结

  • reserve()针对容量,使得之后向其中添加元素时,不需要进行空间的分配。
  • resize()针对元素个数,使得vector的元素为n(n为resize的参数)。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值