演算法導論資料結構篇—vector

來源:互聯網
上載者:User

向量就是動態數組。直接上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;}elseinsert_aux(end(),x);//會配置新空間}void pop_back(){--finish;destroy(finish);}//從position開始,插入n個元素,元素初值為xvoid insert(iterator position,size_type n,const T& x);//在position處插入xvoid 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());elseinsert(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的話,新長度為1iterator new_start=data_allocator::allocate(len);iterator new_finish=new_start;try{//將原vector拷貝到新vectornew_finish=uninitialized_copy(start,position,new_start);//為新元素設定初值construct(new_finish,x);++new_finish;new_finish=uninitialized_copy(position,finish,new_finish);}catch(...){//commit or rollbackdestroy(new_start,new_finish);data_allocator::deallocate(new_start,len);throw;}//析構,釋放原vectordestroy(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 rollbackdestroy(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;}}}//---------------------------------------------------------------------------


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.