vector底层实现

来源:互联网 发布:计算机二级java模拟题 编辑:程序博客网 时间:2024/06/12 01:29

//stl库中vector是一个自动管理的动态数组;

//其实只要明白vector的类型是一个数组,怎么去实现它,其实就好办了;

 

//我选择了一种简单的方式去实现它;//定义一个步长(WALK_LENGTH);

//在数组空间不够的时候,重新申请 allocCount+WALK_LENGTH 长度的内存;

//这样避免了,每次vector元素增加的时候,去重新申请空间;

//当然,不好的地方也很明显。就是会浪费一定的空间。但是时间效率上会提高很多;

//因为vector,可以支持下标访问,而且效率很快,所以没必要去构造一个iterator;

 

//通过分析代码,可以发现vector的优缺点

//1、随即访问元素效率很高;

//2、push_back的效率也会很高;

//3、push_front的效率则非常的低,不建议使用;

//4、insert,需要把插入位置以后的元素,全部后移,效率比较低,不建议使用;

//5、erase,需要把删除位置后面的元素,全部前移,效率比较低,不建议使用;

//6、当内存不够的时候,需要重新申请内存,在把以前的元素拷贝过来,这个时候,效率也是比较低的;

 

//有不足的地方,还希望大家指正

[html] view plaincopy
  1. #ifndef _MY_VECTOR_H_  
  2. #define _MY_VECTOR_H_  
  3.   
  4. #include <assert.h>  
  5.   
  6. template<typename T>  
  7. class myVector  
  8. {  
  9. private:  
  10.     //walk length   
  11.     //myVector each time increase space length  
  12.     #define WALK_LENGTH 64;  
  13.   
  14. public:  
  15.     //default constructor  
  16.     myVector():array(0),count(0),allocCount(0)  
  17.     {  
  18.   
  19.     }  
  20.   
  21.     //  
  22.     myVector(const T& t,unsigned int n):array(0),count(0),allocCount(0)  
  23.     {  
  24.         while(n--)  
  25.         {  
  26.             push_back(t);  
  27.         }  
  28.     }  
  29.   
  30.     //copy constructor  
  31.     myVector(const myVector<T>& other):array(0),count(0),allocCount(0)  
  32.     {  
  33.         *this = other;  
  34.     }  
  35.   
  36.     //= operator  
  37.     myVector<T>operator =(myVector<T>& other)  
  38.     {  
  39.         if(this == &other)  
  40.             return *this;  
  41.   
  42.         clear();  
  43.   
  44.         count = other.size();  
  45.         allocCount = other.allocSize();  
  46.         array = new T[allocCount];  
  47.         for(unsigned int i = 0 ;i<count;++i)  
  48.         {  
  49.             array[i] = other[i];  
  50.         }  
  51.   
  52.         return *this;  
  53.     }  
  54.   
  55.     //destructor  
  56.     ~myVector()  
  57.     {  
  58.         clear();  
  59.     }  
  60.   
  61.     //the pos must be less than myVector.size();  
  62.     T& operator[](unsigned int pos)  
  63.     {  
  64.         assert(pos<count);  
  65.         return array[pos];  
  66.     }  
  67.   
  68.     //element count  
  69.     unsigned int size()  
  70.     {  
  71.         return count;  
  72.     }  
  73.   
  74.     //alloc count  
  75.     unsigned int allocSize()  
  76.     {  
  77.         return allocCount;  
  78.     }  
  79.       
  80.     //is  empty  
  81.     bool empty()  
  82.     {  
  83.         return count == 0;  
  84.     }  
  85.   
  86.     //clear myVector  
  87.     void clear()  
  88.     {  
  89.         deallocator(array);  
  90.         array = 0;  
  91.         count = 0;  
  92.         allocCount = 0;  
  93.     }  
  94.   
  95.     //adds an element in the back of myVector   
  96.     void push_back(const T& t)  
  97.     {  
  98.         insert_after(count-1,t);  
  99.     }  
  100.   
  101.     //adds an element int the front of myVector  
  102.     void push_front(const T& t)  
  103.     {  
  104.         insert_before(0,t);  
  105.     }  
  106.   
  107.     //inserts an element after the pos  
  108.     //the pos must be in [0,count);  
  109.     void insert_after(int pos,const T& t)  
  110.     {  
  111.         insert_before(pos+1,t);  
  112.     }  
  113.   
  114.     //inserts an element before the pos  
  115.     //the pos must be less than the myVector.size()  
  116.     void insert_before(int pos,const T& t)  
  117.     {  
  118.         if(count==allocCount)  
  119.         {  
  120.             T* oldArray = array;  
  121.   
  122.             allocCount += WALK_LENGTH;   
  123.             array = allocator(allocCount);  
  124.             //memcpy(array,oldArray,count*sizeof(T)):  
  125.             for(unsigned int i = 0 ;i<count;++i)  
  126.             {  
  127.                 array[i] = oldArray[i];  
  128.             }  
  129.             deallocator(oldArray);  
  130.         }  
  131.   
  132.         for(int i = (int)count++;i>pos;--i)  
  133.         {  
  134.             array[i] = array[i-1];  
  135.         }  
  136.         array[pos] = t;  
  137.     }  
  138.   
  139.     //erases an element in the pos;  
  140.     //pos must be in [0,count);  
  141.     void erase(unsigned int pos)  
  142.     {  
  143.         if(pos<count)  
  144.         {  
  145.             --count;  
  146.             for(unsigned int i = pos;i<count;++i)  
  147.             {  
  148.                 array[i] = array[i+1];  
  149.             }  
  150.         }  
  151.     }  
  152.   
  153. private:  
  154.     T*  allocator(unsigned int size)  
  155.     {  
  156.         return new T[size];  
  157.     }  
  158.   
  159.     void deallocator(T* arr)  
  160.     {  
  161.         if(arr)  
  162.             delete[] arr;  
  163.     }  
  164. private:  
  165.     T*              array;  
  166.     unsigned int    count;  
  167.     unsigned int    allocCount;  
  168. };  
  169.   
  170. #endif  


//下面的是测试代码

[html] view plaincopy
  1. void printfVector(myVector<int>& vector1)  
  2. {  
  3.     for(unsigned int i = 0 ; i < vector1.size();++i)  
  4.     {  
  5.         cout<<vector1[i]<<",";  
  6.     }  
  7.     cout<<"alloc size = "<<vector1.allocSize()<<",size = "<<vector1.size()<<endl;  
  8. }  
  9.   
  10. void main()  
  11. {  
  12.     myVector<int> myVector1;  
  13.     myVector<int> myVector2(0,10);  
  14.     myVector2.push_front(1);  
  15.     myVector2.erase(11);  
  16.     printfVector(myVector2);  
  17.     myVector1.push_back(2);  
  18.     myVector1.push_front(1);  
  19.     printfVector(myVector1);  
  20.     myVector1.insert_after(1,3);  
  21.     printfVector(myVector1);  
  22.   
  23.     myVector2 = myVector1;  
  24.     myVector2.insert_before(0,0);  
  25.     myVector2.insert_before(1,-1);  
  26.     printfVector(myVector2);  
  27. }  
0 0