运用模板相关知识,仿写模板类vector

vector模板类代码

#include <iostream>

using namespace std;

//模板类循环顺序queue
template <typename T>
class vector
{
    T* start;
    T* last;
    T* end;
public:
    //构造函数
    vector():start(NULL),last(NULL),end(NULL) {}

    //拷贝构造函数
    vector(const vector& other)
    {
        int f = other.last-other.start;
        int h = other.end-other.start;
        start = new T[h];
        last = start+f;
        end = start+h;
        for(int i=0; i<f;i++)
            *(start+i) = *(other.start+i);
    }

    //拷贝赋值函数
    vector& operator=( const vector& other )
    {
        if(this != &other)
        {
            int f = other.last-other.start;
            int h = other.end-other.start;
            start = new T[h];
            last = start+f;
            end = start+h;
            for(int i=0; i<f;i++)
                *(start+i) = *(other.start+i);
        }
        return *this;
    }

    T& operator[]( int loc )
    {
        return start[loc];
    }

    //析构函数
    ~vector(){cout<<"析构函数"<<endl;}

    //构造一个初始放入num个值为val的元素的Vector
    vector(int num, const T &val )
    {
        start = new T[num];
        last = start+num;
        end = start+num;
        for(int i=0;i<num;i++)
            *(start+i) = val;
    }

    //构造一个初始值为[start,end)区间元素的Vector
    vector( T* start, T* end )
    {
        int len = end-start;
        this->start = new T[len];
        this->last = this->start+len;
        this->end = this->start+len;
        for(int i=0;i<len;i++)
            this->start[i] = start[i];
    }

    //返回当前vector所容纳元素的数目
    int size()
    {
        return last-start;
    }

    //返回当前vector在重新进行内存分配以前所能容纳的元素数量
    int capacity()
    {
        return end-start;
    }


    //判空
    bool empty()
    {
        return start==last ?1:0;
    }

    //判满
    bool full()
    {
        return last==end ?1:0;
    }

    //删除最后一个元素
    void pop_back()
    {
        if(!empty())
        {
            *(--last) = (T)NULL;
        }
        else
            cout<<"vector容量为空"<<endl;
    }

    //在vector末尾加入一个元素
    void push_back( const T &val )
    {
        if(!full())
        {
            *last++=val;
        }
        else
        {
            int len = end-start;
            if(len == 0)
            {
                start = new T (val);
                last = start+1;
                end = start+1;
            }
            else{
                T *temp = new T[len*2];
                for(int i=0;i<len;i++)
                    temp[i] = start[i];
                temp[len] = val;
                delete []start;
                start = temp;
                last = start+len+1;
                end = start+len*2;
            }
        }
    }

    //返回当前Vector指定位置loc的元素的引用
    T &at( int loc )
    {
        if(loc >=last-start)
            throw string("数组下标越界");         //在主函数使用try catch函数接收异常
        else
            return start[loc];
    }



    //返回当前vector最末一个元素的引用
    T &back()
    {
        return *(last-1);
    }

    //返回当前vector起始元素的引用
    T &front()
    {
        return *start;
    }

    //返回一个指向当前vector起始元素的迭代器
    T* begin()
    {
        return start;
    }

    //返回一个指向当前vector末尾元素的下一位置的迭代器
    T* _end()
    {
        return last;
    }

    //删除当前vector中的所有元素
    void clear()
    {
        last = start;
    }


};

int main()
{
    vector<int> v1;        //定义一个容器

    cout<<"v1.size = "<<v1.size()<<endl;      //0
    cout<<"v1.capacity = "<<v1.capacity()<<endl;   //0

    cout<<"************************************************************\n";


    //循环插入数据
    for(int i=1; i<=20; i++)
    {
        v1.push_back(i);
        cout<<"v1.size = "<<v1.size()<<"    v1.capacity = "<<v1.capacity()<<endl;
    }


    //输出当前vector中的内容
    for(auto i=0; i<v1.size(); i++)
    {
        //cout<<v1[i]<<" ";
        cout<<v1.at(i)<<" ";
    }
    cout<<endl;


    //调用尾删函数
    v1.pop_back();
    for(int i=0; i<v1.size(); i++)
    {
        //cout<<v1[i]<<" ";
        cout<<v1.at(i)<<" ";
    }
    cout<<endl;


    //将第一个元素的值改为100,最后一个值改为200
    v1.front() = 100;
    v1.back() = 200;
    for(int i=0; i<v1.size(); i++)
    {
        //cout<<v1[i]<<" ";
        cout<<v1.at(i)<<" ";
    }
    cout<<endl;


    //使用迭代器来遍历数组
    for(auto p=v1.begin(); p!=v1._end(); p++)
    {
        cout<<*p<<" ";
    }
    cout<<endl;


    //清空vector
    v1.clear();
    cout<<"v1.size = "<<v1.size()<<endl;      //0
    cout<<"v1.capacity = "<<v1.capacity()<<endl;   //?


    //若不使用try catch则会不执行之后的语句会直接退出
    for(int i=0; i<v1.capacity(); i++)
    {
        //分别使用[]和at来输出看结果如何
        cout<<v1[i]<<" ";
        try {
            cout<<v1.at(i)<<" ";
        } catch (string e) {
            cout<<e;
        }
        cout<<"  ";

    }
    cout<<endl;


    //使用有参构造函数构造一个vector
    vector<char> v2(5, 'A');
    for(int i=0; i<v2.size(); i++)
    {
        cout<<v2[i]<<" ";
        //cout<<v1.at(i)<<" ";
    }
    cout<<endl;

    cout<<"v2.size = "<<v2.size()<<endl;      //5
    cout<<"v2.capacity = "<<v2.capacity()<<endl;   //5
    v2.push_back('G');
    cout<<"v2.capacity = "<<v2.capacity()<<endl;   //10


    //使用别的容器给新容器初始化
    int arr[] = {3,8,4,6,2,9,1,0,7};


    vector<int> v3(arr, arr+5);
    for(int i=0; i<v3.size(); i++)
    {
        cout<<v3[i]<<" ";
        //cout<<v1.at(i)<<" ";
    }
    cout<<endl;

    return 0;
}

测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值