c++ STL之vector的实现

概述

    vector作为STL中最常用的容器之一,他基于数组实现,是一个动态数组,其容量能自动增长,同时也提供了许多操作方法,十分高效实用。那么我们来尝试写一个自己的vector吧!(当然,自己写的肯定没有STL中的高效,就当是一次学习探索吧)

开工!

如何实现动态数组以及如何使其容量自动增长?

    这里我们用指针通过在堆空间上开辟空间来实现动态数组。设置两个变量theSize(当前所含数据个数)和theCapacity(可容纳数据最大数量),当当前数量即将超过最大容量时,重新分配空间。

vector中常用方法

template <typename Object> class Vector{
private :
    int theSize;
    int theCapacity;
    Object *objects;
public:
    typedef Object* iterator;//迭代器
    typedef const Object* const_iterator;
    explicit Vector(int initSize);
    Vector();
    Vector(const Vector<Object>&);
    ~Vector();
    const Vector<Object>&operator=(const Vector<Object>&);
    void push_back(const Object&);//向末尾添加
    void pop_back();//删除最后一个元素
    const Object& back()const;//获取最后一个元素
    const Object& font()const;//获取第一个元素
    Object& back();
    Object& front();
    void resize(int);//重新设置容量
    void reserve(int);//重新设置最大容量
    const int size()const;
    const int capacity()const;
    bool empty();//是否为空
    void clear();//删除所有元素
    Object& at(int);//获取第几个元素
    const Object& at(int)const;
    iterator begin(){return &objects[0];}
    iterator end(){return &objects[size()];}
    const_iterator begin()const{return &objects[0];}
    const_iterator end()const{return &objects[size()];}
    const Object&operator[](int)const;//重载下标运算符
    Object&operator[](int);
};

vector方法的实现

    实现都挺简单的,值得注意的就是在重新分配空间的时候,记得删除掉以前分配的空间,避免堆空间泄露(直接上代码了

template <typename Object> Vector<Object>::Vector():theSize(0),theCapacity(0),objects(NULL) {}
template <typename Object> Vector<Object>::Vector(int initSize) {
    this->theSize=initSize;
    this->theCapacity=this->theSize*2;
    this->objects=new Object[this->theCapacity];
}
template <typename Object> Vector<Object>::Vector(const Vector<Object> &rhs):objects(NULL) {
    operator=(rhs);
}
template <typename Object>Vector<Object>::~Vector() {
    if(objects)delete[]objects;
}
template <typename Object> const Vector<Object>& Vector<Object>::operator=(const Vector<Object> &rhs) {
    if(this!=&rhs){
        theSize=rhs.theSize;
        theCapacity=rhs.theSize;
        if(objects)
            delete[]objects;
        objects=new Object[theCapacity];
        for(int i=0;i<theSize;i++)
            objects[i]=rhs.objects[i];
    }
    return *this;
}
template <typename Object> void Vector<Object>::push_back(const Object &rhs) {
    if(theSize==theCapacity){
        if(theCapacity==0) {
            theCapacity = 1;
            objects =new Object[theCapacity];
        }
        else {
            theCapacity *= 2;
            Object *oldArray = objects;
            objects = new Object[theCapacity];
            for (int i = 0; i < theSize; i++)
                objects[i] = oldArray[i];
            delete[]oldArray;
        }
    }
    this->objects[theSize++]=rhs;
}
template <typename Object> void Vector<Object>::pop_back() {
    this->theSize--;
}
template <typename Object> const int Vector<Object>::size() {
    return this->theSize;
}
template <typename Object> const int Vector<Object>::capacity() {
    return this->theCapacity;
}
template <typename Object> bool Vector<Object>::empty() {
    return theSize==0?true:false;
}
template <typename Object> void Vector<Object>::clear() {
    if(!empty()){
        this->theSize=0;
        this->theCapacity=0;
        delete[]objects;
    }
}
template <typename Object> const Object& Vector<Object>::font()const {
    if(!empty())
        return objects[0];
}
template <typename Object> const Object& Vector<Object>::back()const {
    if(!empty())
        return objects[theSize-1];
}
template <typename Object> void Vector<Object>::resize(int newSize){
    if(newSize>theSize)
        for(int i=theSize;i<newSize;i++)
            objects=Object();
    this->theSize=newSize;
}
template <typename Object> void Vector<Object>::reserve(int newCapacity) {
    if(newCapacity<theCapacity)
        return;
    this->theCapacity=newCapacity;
    Object*oldArray=objects;
    objects=new Object[theCapacity];
    for(int i=0;i<theSize;i++)
        objects[i]=oldArray[i];
    delete[]oldArray;
}
template <typename Object> Object& Vector<Object>::at(int index) {
    if(index>=theSize||index<0)
        throw "Out of Range";
    return objects[index];
}
template <typename Object>const Object& Vector<Object>::at(int index)const {
    if(index>=theSize||index<0)
        throw "out of range";
    return objects[index];
}
template <typename Object>const Object& Vector<Object>::operator[](int index)const {
    if(index>=theSize||theSize<0)
        throw "out of range";
    return objects[index];
}
template <typename Object>Object& Vector<Object>::operator[](int index) {
    if(index>=theSize||theSize<0)
        throw "out of range";
    return objects[index];
}

最后

    一个vector差不多就这样实现了,如果有能改进的地方,希望大家不吝赐教

发布了18 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Juicewyh/article/details/82828770