STL vector 扩容

昨天面试,面试官问到一个问题,vector怎么扩容的,我大概记得C++ premier有那么一句扩两倍。我说好像是扩两倍,面试官也没继续问也没反馈对错。甚是好奇对错,索性自己试验一把,并看下源码。

试验

代码

#include<iostream>
#include<vector>
using namespace std;

int main(int argc, char *argv[])
{
	vector<int> vInt;

	for(int i = 0; i < 100; i++ )
	{
		cout << "size:" << vInt.size() << "\t" << "capacity:" << vInt.capacity() << endl;
		vInt.push_back(1);
	}

	return 0;
}

输出

size:0	capacity:0
size:1	capacity:1
size:2	capacity:2
size:3	capacity:4
size:4	capacity:4
size:5	capacity:8
size:6	capacity:8
size:7	capacity:8
size:8	capacity:8
size:9	capacity:16
size:10	capacity:16
size:11	capacity:16
size:12	capacity:16
size:13	capacity:16
size:14	capacity:16
size:15	capacity:16
size:16	capacity:16
size:17	capacity:32
size:18	capacity:32
......

源码

在这只把扩容相关代码贴出来罢了。

//SGI STL v3.3
void push_back(const _Tp& __x) {
if (_M_finish != _M_end_of_storage) {
  construct(_M_finish, __x);
  ++_M_finish;
}
else
  _M_insert_aux(end(), __x);
}

iterator insert(iterator __position, const _Tp& __x) {
size_type __n = __position - begin();
if (_M_finish != _M_end_of_storage && __position == end()) {
  construct(_M_finish, __x);
  ++_M_finish;
}
else
  _M_insert_aux(__position, __x);
return begin() + __n;
}

template <class _Tp, class _Alloc>
void 
vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x)
{
  if (_M_finish != _M_end_of_storage) {
    construct(_M_finish, *(_M_finish - 1));
    ++_M_finish;
    _Tp __x_copy = __x;
    copy_backward(__position, _M_finish - 2, _M_finish - 1);
    *__position = __x_copy;
  }
  else {
    const size_type __old_size = size();
    //原容量为0扩为1,否则扩为原2倍
    const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
    iterator __new_start = _M_allocate(__len);
    iterator __new_finish = __new_start;
    __STL_TRY {
      __new_finish = uninitialized_copy(_M_start, __position, __new_start);
      construct(__new_finish, __x);
      ++__new_finish;
      __new_finish = uninitialized_copy(__position, _M_finish, __new_finish);
    }
    __STL_UNWIND((destroy(__new_start,__new_finish), 
                  _M_deallocate(__new_start,__len)));
    destroy(begin(), end());
    _M_deallocate(_M_start, _M_end_of_storage - _M_start);
    _M_start = __new_start;
    _M_finish = __new_finish;
    _M_end_of_storage = __new_start + __len;
  }
}

在push_back或者inster的时候,vetor容量不够扩容为原长度两倍。
这个增长因子和各版本STL实现有关,有的版本用1.5

为什么是扩为原长度2倍

因为每次扩容都涉及到原来元素的copy。空间和时间的权衡。
简单来说, 空间分配的多,平摊时间复杂度低,但浪费空间也多。
具体参见算法导论中,平摊分析那一章关于动态表扩张的分析。

此条目发表在STL分类目录,贴了, 标签。将固定链接加入收藏夹。

STL vector 扩容》有1条回应

  1. davelv说:

    STL里面的有序容器的扩展都是一个模子,vector如此,hashmap也是如此。
    生成了新的空间,然后再挨个插入过去。

发表评论

您的电子邮箱地址不会被公开。