序列式容器
序列式容器:可序列集群,其中的元素都可序,但未必有序。
vector概述
vector的数据安排以及操作方式,与array非常相似。两者的唯一差别在于空间的运用的灵话性,array 是静态空间,一旦配置了就不能改变;要换个大(或小)一点的房子,可以,一切琐细得由客户端自己来:首先配置一块新空间, 然后将元素从旧址搬往新址, 再把原来的空间释还给系统。 vector 是动态空间,随着元素的加人,它的内部机制会自行扩充空间以容纳新元素。因此,vector 的运用对于内存的合理利用与运用的灵活性有很大的帮助,我们再也不必因为害怕空间不足而一开始就要求 一个大块头array 了,我们可以安心使用vector,吃多少用多少。
vector的实现技术,关键在于其对大小的控制以及重新配置时的数据移动效率.一旦vector旧有空间满载,如果客户端每新增一个元素,vector内部只是扩充一个元素的空间,实为不智,因为所谓扩充空间(不论多大) .一如精早所说,是“配置新空间/数据移动,释还旧空间”的大工程,时间成本很高,应该加入某种未雨绸缪的考虑。
vector定义源码摘录
// alloc是SGI STL的空间配置器
template <class T, class A11oc . alloc>
class vector {
public:
// vector的嵌套型别定义
typedef T value_ type;
typedef value_ type* pointer;
typedef value_ type* iterator ;
typedef value_ type& reference;
typedef size_ .t size_ type;
typedef ptrdiff_ _t di fference_ type;
protected:
//以下,simple. allc是sGI STL的空间配置器
typedef simple_alloc<value_type,Alloc>data_allocator;
iterator start;//表示目前使用空间的头
iterator finish;//表示目前使用空间的尾
iterator end_of_storage; //表示目前可用空间的尾
void insert_ aux(iterator position, const T& x) ;
void deallocate()
{
if (start)
data_ ,allocator: :deallocate(start, end_ _of_ storage - startl;
}
void fill_ initialize(size_ type n, const T& value)
{
start = allocate_ ,and_ fill(n, value) ;finish = start + n;
end_ of_ storage = finish;
}
public:
iterator begin() {return start;}
iterator end() { return finish;}
size_ type size() const { return size_ .typelend() - begin());}
size_ type capacity() const{ return size_ type(end_ of_ storage begin());}
bool empty() const { return beginl) == end();}
reference operator[](size_type n){ return *(begin() + n); }
vector() : start(0), finish(0), end_ of_ storage(0) {}
vector(size_ type n, const T& value) {fill_ initialize(n, value);}
vector(int n, const T& value) { fill_ initialize(n, value); }
vector(long n, const T& value) { fill_ initialize(n, value); }
explicit vector(size_ .type n) { fill_ initialize(n, T()); }
~vector() {
destroy (start, finish);
deallocate();}
reference front(){return *begin();}//第一个 元素
reference back(){return *(end()-1);}//最后一个元素
void push_back(const T& x)
{
if(finsh !=end_of_storage)
{
construct(finsh, x);
++finish;
}
else
insert_aux(end(),x);
}
void pop_back()
{
--finish;
destory(finish);
}
iterator erase(iterator position)
{
if(position+1!=end())//清除某位置上的元素
{
copy(position+1,finish,position);
}
--finish;//后续元素往前移动
destory(finish);
return position;
}
void resize(size_type new_ size, const T& x)
{
if (new_ size < sizel)
erase(beginl + new_ size, endll;
else
insert(endl), new_ size - sizel), x);
}
void resize(size_ _type new_ size) {resize(new_ size, TIl; }
void clear() {erase(beginl), end(l);}
protected:
//配置空间并填满内容
iterator allocate_ and_ fill(size_ type n, const T& x)
{
iterator result = data_ allocator: :allocate(n) ;
uninitialized_ fill_ n(result, n, x);
return result;
}
vector的迭代器
vector 维护的是一个连续线性空间,所以不论其元素型别为何种,普通指针都可以作为vector的迭代器而满足所有必要条件。
vector<int>::iterator ac;
vector<Shape>::iterator ab;
//ac 的型别就是int*;ab的型别就是Shape*。
vector的数据结构
vector 所采用的的数据结构非常简单:线性连续空间,它的两个迭代器start和finish分别指向配置得来的连续空间中目前已使用的范围,并以迭代器end_of_storage指向整块连续的空间的尾端,如下图所示: