模板顺序表

用模板写函数或类都与类型无关,因此,STL中都是用模板实现容器,下面我们来介绍用模板实现顺序表。
关于模板的知识,在之前的博客中有提到:
http://blog.csdn.net/gatieme/article/details/51383272

#include<iostream>
using namespace std;
#include<string>

//模板顺序表
template<class T>

class Vector
{
    typedef T* Iterator;
    typedef const T* ConstIterator;
public:
    Vector()
        :_start(0)
        , _finish(0)
        , _endofstorage(0){}

    Vector(const T* arr, size_t size)//非类型参数模板类构造函数
        :_start(new T[size])
        , _finish(_start)
        , _endofstorage(_start + size)
    {
        for (size_t idx = 0; idx < size, ++idx)
        {
            _start[idx] = arr[idx];
        }
    }

    Vector(const  Vector<T> &v)//拷贝构造函数
        :_start(new T[v._endofstorage - v._start])
    {
        size_t size = v._finish - v._start;
        size_t capacity = v._endofstorage - v._start;
        _start = new[capacity];
        _endofstorage = _start + capacity;
        _finish = _start + size;
        for (size_t idx = 0; idx < size; ++idx)
        {
            _start[idx] = v._start[idx];
        }
    }

    Vector <T>& operator = (const Vector <T> &v)//赋值运算符的重载
    {
        if (this != &v)
        {
            Vector<int>temp(v);
            swap(temp);
        }
        return *this;
    }

    void swap(Vector<T> &v)
    {
        std::swap(_start, v._start);
        std::swap(_finish, v._finish);
        std::swap(_endofstorage, v._endofstorage);
    }

    ~Vector()
    {
        if (_start)
        {
            delete[] _start;
            _start = 0;
            _finish = 0;
            _endofstorage = 0;
        }
    }

    Iterator End()
    {
        return _finish;
    }

    Iterator Begin()
    {
        return _start;
    }

    ConstIterator Begin()
    {
        return _start;
    }

    ConstIterator End()const
    {
        return _finish;
    }

    size_t size()const//大小
    {
        return _finish - _start;
    }

    size_t Capacity()const//容量
    {
        return _endofstorage - _start;
    }

    T& operator[](size_t index)
    {
        return _start[index];
    }

    const T& operator[](size_t index)//下标
    {
        return _start[index];
    }

    T& Back()
    {
        return *(--End);
    }

    const T& Back()//最后一个元素
    {
        return *(--End);
    }

    void checkcapacity()//检查容量
    {
        if (_finiah == _endofstorage)
        {
            size_t capacity = 2 * (_endofstorage - _start) + 3;
            T* temp = new T [Capacity];
            size_t = 0;
            T* pos = _start;
            while (pos != _endofstorage)
            {
                temp[idx++] = pos++;
                delete[] _start;
                _start = temp;
                _finish = _start + idx;
                _endofstorage = _start + capacity;
            }
        }

        T& front()//第一个元素
        {
            return *Begin;
        }

        void PushBack(const T& x)//后插
        {
            checkcapacity();
            *_finish = x;
        }

        void PopBack()//后删
        {
            --finish;
        }

        Iterator Insert(Iterator pos, const T& x)//任意位置插入
        {
            checkcapacity();
            Iterator it = _finish;
            while (it > pos)
            {
                *it = *(it - 1);
                --it;
            }
            *pos = x;
            _finish++;
            return pos;
        }

        Iterator Erase(Iterator pos)//任意位置删除
        {
            Iterator it = pos;
            while (it < (_finish - 1))
            {
                *(it) = *(it + 1);
                ++it;
            }
            --finish;
            return pos;
        }
    }

private:
    T * _start;
    T * _finish;
    T * _endofstorage;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值