首页 > 代码库 > 数据结构One_Vector(向量的简单实现)

数据结构One_Vector(向量的简单实现)

  1 #include <iostream>  2 using namespace std;  3   4 template<typename Object>  5 class  Vector  6 {  7 private:  8     int theSize;                         //实际数据大小  9     int theCapacity;                     //实际容器容量大小 10     Object *objects;                     //基本数组 11 public: 12     enum { SPACE_CAPACITY = 16 };        //默认容量大小 13      14     explicit Vector(int initSize = 0)    //单参数构造函数要用explicit()避免类型在后台转换 15         : theSize(initSize), theCapacity(initSize + SPACE_CAPACITY) { 16         objects = new Object[theCapacity]; 17     } 18     Vector(const Vector& rhs) : objects(NULL) {    //复制构造函数--调用operator=对已有的Vector进行复制 19         operator = (rhs); 20     } 21     ~Vector() { 22         delete[] objects; 23     } 24  25     const Vector& operator = (const Vector& rhs) //重载赋值运算符 26     { 27         if (this != &rhs)                        //避免复制自身--混淆检验 28         { 29             delete []objects;                    //删除旧的内存空间 30             theSize = rhs.size();                //生成同样的样本大小 31             theCapacity = rhs.theCapacity;       //生成同样的容量大小 32  33             objects = new Object[capacity()];    //生成与所复制的Vector同样容量的新数组 34             for (int k = 0; k < size(); k++) 35                 objects[k] = rhs.objects[k];  36         } 37         return *this; 38     } 39      40     void resize(int newSize) 41     { 42         if (newSize > theCapacity)        //重置大小 43             reserve(newSize * 2 + 1);     //新大小 44         theSize = newSize; 45     } 46  47     void reserve(int newCapacity) 48     { 49         if (newCapacity < theSize)        //至少和(样本大小)一样大 50             return; 51  52         Object *oldArray = objects;       //oldArray--用于复制旧数组内容 53         objects = new Object[newCapacity]; 54         for (int k = 0; k < theSize; k++) 55             objects[k] = oldArray[k]; 56  57         theCapacity = newCapacity; 58         delete []oldArray; 59     } 60  61     Object& operator[] (int index) 62     { 63         return objects[index]; 64     } 65     const Object& operator[] (int index) const 66     { 67         return objects[index]; 68     } 69  70     bool empty() const { 71         return size() == 0; 72     } 73      74     int size() const { 75         return theSize; 76     } 77     int capacity() const { 78         return theCapacity; 79     } 80     void push_back(const Object& x) { 81         if (theSize == theCapacity) 82             reserve(2 * theCapacity + 1); 83         objects[theSize++] = x; 84     } 85      86     void pop_back() { 87         theSize--; 88     } 89     const Object& back() const { 90         return objects[theSize - 1]; 91     } 92      93     typedef Object *iterator; 94     typedef const Object *const_iterator; 95  96     iterator begin() { 97         return &objects[0]; 98     } 99     const_iterator begin() const {100         return &objects[0];101     }102     iterator end() {               //尾后的不存在的指针103         return &objects[size()];     104     }105     const_iterator end() const {106         return &objects[size()];107     }108 };109 110 int main()111 {112     Vector<int> test;113     int data;114     while (cin >> data)115     {116         test.push_back(data);117     }118     Vector<int>::iterator it;119     for (it = test.begin(); it != test.end(); ++it)120         cout << *it << " ";121     cout << endl;122     cout << "pop_one.....\n";123     test.pop_back();124     cout << test.back() << endl;125     return 0;126 }

 

数据结构One_Vector(向量的简单实现)