11.11作业

#include <iostream>
using namespace std;
typedef int size_type;

template <typename T>
class my_vector
{
private:
    T *last;
    T *frist;
    T *end;
public:
    my_vector()
    {
        last=NULL;
        frist=NULL;
        end=NULL;
    }
    ~ my_vector()
    {
        delete []frist;
        last=NULL;
        frist=NULL;
        end=NULL;
    }
    my_vector(size_type a,T data)
    {
        frist=new T[a];
        end=last=frist+a;
        for(int i=0;i<a;i++)
        {
            frist[i]=data;
        }
    }
    my_vector(T *f_p,T *l_p)
    {
        
        frist=new T[l_p-f_p];
        last=end=frist+(l_p-f_p);
        for(int i=0;i<(l_p-f_p);i++)
        {
            frist[i]=f_p[i];
        }
    }
    bool empty()
    {
        return (frist==NULL)||frist==last?true:false;
    }
    size_type capacity()
    {
        size_type len=end-frist;
        return len;
    }
    size_type size()
    {
        return last-frist;
    }
    void space_x2()
    {
        size_type len=(end-frist);
        T *temp=frist;
        size_type len1=(last-frist);
        frist=new T[len*2];
        last=len1+frist;
        end=frist+2*len;
        memcpy(frist,temp,len*sizeof (T));
        delete []temp;
    }
    void push_back( const size_type &val )
    {
        if(frist==NULL)
        {
            frist=new T(val);
            last=end=frist+1;
        }
        else if(end==last)
        {
            space_x2();
            *last=val;
            last++;
        }
        else
        {
            *last=val;
            last++;
        }
    }
    void pop_back()
    {
        if(empty())
        {
            cout<<"当前数组为空"<<endl;
            return;
        }
        else
        {
            last--;
        }
    }
    T at( size_type loc )
    {
        if(loc>=(last-frist)||loc<0)
        {
            cout<<"输入数据不合理"<<endl;
        }
        else
        {
            return frist[loc];
        }
    }
    T front()
    {
        if(empty())
        {
            cout<<"数组为空"<<endl;
        }
        else
        {
            return *frist;
        }
    }
    T back()
    {
        if(empty())
        {
            cout<<"数组为空"<<endl;
        }
        else
        {
            return *(last-1);
        }
    }
    T* begin()
    {
        if(empty())
        {
            cout<<"数组为空"<<endl;
        }
        else
        {
            return frist;
        }
    }
    T* end_m()
    {
        if(empty())
        {
            cout<<"数组为空"<<endl;
        }
        else
        {
            return last;
        }
    }
    my_vector & operator=(const my_vector &v)
    {
        frist=new  T[v.end-v.frist];
        memcpy(frist,v.frist,(v.last-v.frist)*sizeof (T));
        last=frist+(v.last-v.frist);
        end=frist+v.end-v.frist;
        return *this;
    }
    T operator[](size_type a)
    {
        return frist[a];
    }
    void clear()
    {
        delete []frist;
        end=last=frist=NULL;
    }
    
    
};


int main()
{
    my_vector<int> v1;        //无参构造一个vector对象
    
    //判断容器内是否为空
    if(v1.empty())
    {
        cout<<"empty"<<endl;
    }else
    {
        cout<<"not empty"<<endl;
    }
    cout<<"size of v1:"<<v1.size()<<endl;
    cout<<"capacity of v1:"<<v1.capacity()<<endl;
    cout<<"**************************"<<endl;
    for(int i=0; i<5; i++)
    {
        v1.push_back(i+10);
        cout<<"capacity of v1:"<<v1.capacity()<<endl;
    }
    cout<<"size of v1:"<<v1.size()<<endl;
    v1.pop_back();
    cout<<"size of v1:"<<v1.size()<<endl;
    for(int i=0; i<v1.size(); i++)
    {
        //cout<<v1.at(i)<<" ";
        cout<<v1[i]<<" ";
    }
    cout<<endl;
    cout<<"the first one:"<<v1.front()<<endl;
    cout<<"the last one:"<<v1.back()<<endl;
    
    for( auto it=v1.begin(); it!=v1.end_m(); it++ )
        
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    v1.clear();
    cout<<"size of v1:"<<v1.size()<<endl;
    cout<<"*********************************************************"<<endl;
    my_vector<char> v2(5, 'K');
    for( auto it=v2.begin(); it!=v2.end_m(); it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    cout<<"*********************************************************"<<endl;
    int arr[10] = {2,3,6,8,4,5,1,9,37,7};
    my_vector<int> v3(arr, arr+6);
    for( auto it=v3.begin(); it!=v3.end_m(); it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值