首页 > 代码库 > Vector的实现

Vector的实现

#include <cassert> template <class object>class Vector {public:    explicit Vector(int initsize = 0)        : _size(initsize), _capacity(initsize + SPARE_CAPACITY)            {_object = new object[_capacity];}    Vector(const Vector &rhs) : _object(NULL)         { operator=(rhs);}    ~Vector()        {delete [] _object;}            const Vector& operator=(const Vector &rhs)    {        if(this != &rhs)            {                delete [] _object;                _size = rhs._size;                _capacity = rhs._capacity;                                _object = new object[_capacity];                for(int k = 0; k < _size; k++)                    _object[k] = rhs._object[k];                }            return *this;    }        void resize(int newSize)    {        if(newSize > _capacity)            reserve(newSize * 2 + 1);        _size = newSize;    }        void reserve(int newCapacity)    {        if(newCapacity < _size)            return;                object *oldobject = _object;        _object = new object[newCapacity];        for (int n=0; n<_size; n++)            _object[n] = oldobject[n];        delete [] oldobject;        _capacity = newCapacity;    }        object& operator[](const int index)    {        assert(index >= 0 && index < _size);        return _object[index];    }        const object& operator[](const int index) const    {        assert(index >= 0 && index < _size);        return _object[index];    }        bool empty() const     {return _size == 0;}        int size() const     {return _size;}        int capacity() const     {return _capacity;}        void push_back(const object &x)    {        if(_size == _capacity)            reserve(2*_capacity + 1);        _object[_size++] = x;    }        void pop_back()        {_size--;}        const object& back() const         { return _object[_size - 1];}        typedef object* iterator ;    typedef const object* const_iterator;        iterator begin()    {return &_object[0];}        iterator end()    {return     &_object[_size];}    const_iterator begin() const    {return &_object[0];}        const_iterator end() const    {return &_object[_size];}        enum{ SPARE_CAPACITY = 16};    private:    int _size;    int _capacity;    object *_object;    };

 

测试程序:

// output// sizeof(vint) is: 12// size of vint is: 24// capacity of vint is: 27// size of vint2 is: 48// capacity of vint2 is: 97// the last element of vint is: 23// the 5 element of vint is: 4// 012345678910111213141516171819202122// [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]// [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1330118777 1380205901 1027954249 1207974467 1346719055 1028150337 1702057308 1247572850 2037214569 1129270272 1346456641 1413563472 977485121 1702057308 1247572850 2037214569 1886404956 1635017028 1668238428 1275096161 1313818447 1448232275 1547522629 1296648796]// [Finished in 0.9s]#include <iostream>#include "printCollection.h"#include "c2_vector.h"using namespace std;int main(){    //default constructor    Vector<int> vint;    Vector<int> vint2;        //calc class size, two int, one pointer, so size is 12    cout <<"sizeof(vint) is: " << sizeof(vint) << endl;        //void push_back(const object &x)    for (int n=0; n<24; n++)    {        vint.push_back(n);    }        //const Vector& operator=(const Vector &rhs)    vint2 = vint;        //void resize( int newSize)    vint2.resize(48);        //int size(); int capacity();    cout << "size of vint is: " << vint.size() << endl;    cout << "capacity of vint is: " << vint.capacity() << endl;    cout << "size of vint2 is: " << vint2.size() << endl;    cout << "capacity of vint2 is: " << vint2.capacity() << endl;        //const object& back();    cout << "the last element of vint is: " << vint.back() << endl;        //object& operator[](const int index)    cout << "the 5 element of vint is: " << vint[4] << endl;        //void pop_back()    vint.pop_back();        //typedef object* iterator; typedef const ojbect* const_iterator    //iterator begin(); iterator end();    for (Vector<int>::iterator iter = vint.begin(); iter != vint.end(); iter++)    {        cout << *iter;    }    cout << endl;        printCollection(vint);     printCollection(vint2); }
// output// sizeof(vint) is: 12// size of vint is: 24// capacity of vint is: 27// size of vint2 is: 48// capacity of vint2 is: 97// the last element of vint is: 23// the 5 element of vint is: 4// 012345678910111213141516171819202122// [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]// [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1330118777 1380205901 1027954249 1207974467 1346719055 1028150337 1702057308 1247572850 2037214569 1129270272 1346456641 1413563472 977485121 1702057308 1247572850 2037214569 1886404956 1635017028 1668238428 1275096161 1313818447 1448232275 1547522629 1296648796]// [Finished in 0.9s]#include <iostream>#include "printCollection.h"#include "c2_vector.h"using namespace std;int main(){    //default constructor    Vector<int> vint;    Vector<int> vint2;        //calc class size, two int, one pointer, so size is 12    cout <<"sizeof(vint) is: " << sizeof(vint) << endl;        //void push_back(const object &x)    for (int n=0; n<24; n++)    {        vint.push_back(n);    }        //const Vector& operator=(const Vector &rhs)    vint2 = vint;        //void resize( int newSize)    vint2.resize(48);        //int size(); int capacity();    cout << "size of vint is: " << vint.size() << endl;    cout << "capacity of vint is: " << vint.capacity() << endl;    cout << "size of vint2 is: " << vint2.size() << endl;    cout << "capacity of vint2 is: " << vint2.capacity() << endl;        //const object& back();    cout << "the last element of vint is: " << vint.back() << endl;        //object& operator[](const int index)    cout << "the 5 element of vint is: " << vint[4] << endl;        //void pop_back()    vint.pop_back();        //typedef object* iterator; typedef const ojbect* const_iterator    //iterator begin(); iterator end();    for (Vector<int>::iterator iter = vint.begin(); iter != vint.end(); iter++)    {        cout << *iter;    }    cout << endl;        printCollection(vint);     printCollection(vint2); }