向量就是动态数组。直接上stl源码:
//myvector.h
#include"mymemory.h"
#include"myiterator.h"
#include"myconstruct.h"
using std::copy_backward;
using std::max;
template<class T,class Alloc=alloc>
class vector
{
public:
typedef T value_type;//此句定义之后,所以的类型都与value_type而非T有关
typedef value_type* pointer;
typedef value_type* iterator;
typedef value_type& reference;
typedef size_t size_type;
typedef ptrdiff_t defference_type;
protected:
//定义属于自己的配置器
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-start);
}
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_type(finish-start);}
size_type capacity() const{return size_type(end_of_storage-begin());}
size_type empty() const{return begin()==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);}
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(finish!=end_of_storage){
construct(finish,x);//全局函数
++finish;
}else
insert_aux(end(),x);//会配置新空间
}
void pop_back(){--finish;destroy(finish);}
//从position开始,插入n个元素,元素初值为x
void insert(iterator position,size_type n,const T& x);
//在position处插入x
void insert(iterator position,const T& x){ insert(position,1,x);}
iterator erase(iterator position){//清除某位置上的元素
if(position+1!=end())
copy(position+1,finish,position);
--finish;
destroy(finish);
return position;
}
iterator erase(iterator first,iterator last);
void resize(size_type new_size,const T& x)
{
if(new_size<size())
erase(begin()+new_size,end());
else
insert(end(),new_size-size(),x);
}
void resize(size_type new_size){ resize(new_size,T());}
void clear(){erase(begin(),end());}
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;
}
};
//--------------------------------------------------------------------
template<typename T,class Alloc>
void vector<T,Alloc>::insert_aux(iterator position,const T& x)
{
if(finish!=end_of_storage){//还有空间
construct(finish,*(finish-1));//在备用空间除,构造一个元素,其值为vector中最后一个元素
++finish;
T x_copy=x;
copy_backward(position,finish-2,finish-1);
*position=x_copy;
}else{
const size_type old_size=size();
const size_type len=(old_size!=0)?2*old_size:1;//如果原来大小是0的话,新长度为1
iterator new_start=data_allocator::allocate(len);
iterator new_finish=new_start;
try{
//将原vector拷贝到新vector
new_finish=uninitialized_copy(start,position,new_start);
//为新元素设定初值
construct(new_finish,x);
++new_finish;
new_finish=uninitialized_copy(position,finish,new_finish);
}catch(...){
//commit or rollback
destroy(new_start,new_finish);
data_allocator::deallocate(new_start,len);
throw;
}
//析构,释放原vector
destroy(begin(),end());
deallocate();
start=new_start;
finish=new_finish;
end_of_storage=new_start+len;
}
}
//-------------------------------------------------------------------------------
template<typename T,typename Alloc>
typename vector<T,Alloc>::iterator vector<T,Alloc>::erase(iterator first,iterator last)
{
iterator i=copy(last,finish,first);
destroy(i,finish);
finish=finish-(last-first);
return first;
}
//----------------------------------------------------------------------------
//这段代码我觉得写得非常不好,因为进行数据移动时,明明可以一次移动成功的,
//却非得分段多次移动
template<typename T,typename Alloc>
void vector<T,Alloc>::insert(iterator position,size_type n,const T& x)
{
if(n!=0){
if(size_type(end_of_storage-finish)>=n){
//备用空间足够
T x_copy=x;
const size_type elems_after=finish-position;
iterator old_finish=finish;
if(elems_after>n){
//插入点之后的现有元素个数大于新增元素个数
uninitialized_copy(finish-n,finish,finish);
finish+=n;
copy_backward(position,old_finish-n,old_finish);
fill(position,position+n,x_copy);
}else{
//插入点之后的现有元素个数小于等于新增元素个数
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_n(new_finish,n,x);
new_finish=uninitialized_copy(position,finish,new_finish);
}catch(...){
//commit and rollback
destroy(new_start,new_finish);
data_allocator::deallocate(new_start,len);
throw;
}
destroy(start,finish);
deallocate();//重包装过的函数
start=new_start;
finish=new_finish;
end_of_storage=new_start+len;
}
}
}
//---------------------------------------------------------------------------