STL最简单的容器vector
STL的vector实现了一个可以自动增长,快速随机访问的数组,使用效率不俗,搭载STL算法使用方便,是日常最常用的数据结构之一。擅长在尾部进行插入,删除,随机访问。在容器中间删除或者容器扩容时,需要进行对象迁移。
vector实现的基本思路
连续分配地址空间,分别用start finish end_of_storage三个迭代器指示元素开始 元素结束 地址结束,对容器的其他所有的算法操作都是基于这三个迭代器来进行的。下面将会详细的描述。
vector具体实现
以下是vector中较为简单的函数,单独归拢在一起。
template <class T, class Alloc = alloc>
class vector {
public:
typedef T value_type;
typedef value_type* pointer;
// 可以看出vector的迭代器类型其实是原生指针,是随机访问迭代器
typedef value_type* iterator;
typedef value_type& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
protected:
typedef simple_alloc<value_type, Alloc> data_allocator;
iterator start; // 存放元素的开始
iterator finish; // 存放元素的末尾
iterator end_of_storage; // 容器最大容量的末尾
public:
iterator begin() { return start; }
iterator end() { return finish; }
size_type size() const { return size_type(end() - begin()); }
size_type capacity() const { return size_type(end_of_storage - begin()); }
void resize(size_type new_size, const T& x = value_type())
{
if (new_size < size())
erase(begin() + new_size, end());
else
insert(end(), new_size - size(), x);
}
bool empty() const { return begin() == end(); }
reference front() { return *begin(); }
reference back() { return *(end() - 1); }
reference operator[](size_type n) { return *(begin() + n); }
};
下面是vector的构造和析构函数的具体实现
vector() : start(0), finish(0), end_of_storage(0) {}
vector(size_type n, const T& x) { fill_initialize(n, x); }
vector(size_type n) { fill_initialize(n, T()); }
// 析构分为两步
~vector() { destroy(start, finish); deallocate();}
void deallocate()
{
if (start)
data_allocator::deallocate(start, end_of_storage - start);
}
void fill_initialize(size_type n, const T& value)
{
start = allocate_and_fill(n, value);
finish = start + n;
end_of_storage = finish;
}
iterator allocate_and_fill(size_type n, const T& value)
{
// 分配可容纳n个对象的空间
iterator result = data_allocator::allocate(n);
// 开始在这片空间上构造n个对象
uninitialized_fill_n(result, n, value);
return result;
}
其他重要函数的实现
void push_back(const T& x)
{
// 上次分配的空间还有余量,不用腾挪
if (finish != end_of_storage)
{
construct(finish, x);
++finish;
}
else
insert_aux(end(), x);
}
void insert_aux(iterator position, const T& x)
{
if (finish != end_of_storage)
{
construct(finish, *(finish - 1));
++finish;
T x_copy = x;
// 从后向前,把[position,finish-2]的元素拷贝到finish-1
copy_backward(position, finish - 2, finish - 1);
*position = x_copy;
}
else
{
const size_type old_size = size();
// 空间不够,2倍扩容
const size_type len = old_size != 0 ? 2 * old_size : 1;
// 开辟新的够大的空间
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
try {
// step 1:把原数组[start,position]拷贝过来
new_finish = uninitialized_copy(start, position,new_start);
// step 2:把要插入的元素拷贝过来
construct(new_finish,x);
// step 3:调整new_finish
++new_finish;
// step 4:把原数组剩余元素拷贝过来
new_finish = uninitialized_copy(position, finish, new_finish);
}
catch(...){}
// 析构原数组对象,回收原数组空间,设置新的begin,finish
destroy(begin(), end());
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
void pop_back()
{
--finish;
destroy(finish);
}
iterator erase(iterator first, iterator last)
{
iterator i = copy(last, finish, first);
destroy(i, finish);
finish = finish - (last - first);
return first;
}
iterator erase(iterator position)
{
if (position + 1 != end())
{
copy(position + 1, finish, position);
}
--finish;
destroy(finish);
return position;
}
void clear() { erase(begin(), end()); }
void insert(iterator position, size_type n, const T& x)
{
if (n != 0)
{
if (size_type(end_of_storage - finish) >= n) {
// seq:0 1 2 3 4 5 6 7 8 9 _
// position=begin+2=2,n=3,x=8
// elems_after=finish-position=10-2=8
// old_finish=finish = 10
T x_copy = x;
const size_type elems_after = finish - position;
iterator old_finish = finish;
if (elems_after > n)
{
// finish-n=7 finish = 10
// seq:0 1 2 3 4 5 6 7 8 9 _
// finish += 3 ==> finish = 13
// seq:0 1 2 3 4 2 3 4 5 6 7 8 9 _
// seq:0 1 8 8 8 2 3 4 5 6 7 8 9 _
uninitialized_copy(finish - n, finish, finish);
finish += n;
copy_backward(position, old_finish-n, old_finish);
fill(position, position+n, x_copy);
}
else
{
// seq:0 1 2 3 4 5 6 7 8 9 _
// position=begin+8,n=3,x=2
// elems_after=finish-position=2
// seq:0 1 2 3 4 5 6 7 8 9 2 _
// finish += n - elems_after ==> finish = 11
// seq:0 1 2 3 4 5 6 7 8 9 2 8 9 _
// finish += elems_after ==> finish = 13
// seq:0 1 2 3 4 5 6 7 2 2 2 8 9 _
uninitialized_fill_n(finish, n-elems_after,x_copy);
finish += n - elems_after;
uninitialized_copy(position, old_finish, finish);
finish += elems_after;
fill(position,old_finish,x_copy);
}
}
else {
const size_type old_size = size();
const size_type len = old_size + max(old_size, n);
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
try {
new_finish = uninitialized_copy(start,position,new_start);
new_finish = uninitialized_fill(new_finish,n,x);
new_finish = uninitialized_copy(position,finish,new_finish);
}
catch(...){}
destroy(start,finish);
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
}
实例及注意事项
- vector操作内部并没有考虑越界的情况,使用请小心
- vector的容量是2倍增长的
- vector的使用量和总容量不一定相等
- 待补充
网友评论