2022.12.10---标准模板库

本文介绍了一个自定义的Vector类实现,包括基本的构造函数、析构函数、拷贝构造函数,以及常见的容器操作如push_back、pop_back等。通过实例演示了如何使用该Vector类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

封装自己的vector

#include <iostream>
#include<vector>

using namespace std;

template<typename type>

class my_vector
{
private:
    type *data;
    type *last;
    type *tail;
    int num;
public:
    my_vector():data(NULL),last(NULL),tail(NULL),num(0) {}       //无参构造
    my_vector(int n,const type &var):num(n)      //有参构造
    {
        data=new type[num];
        for(int i=0;i<num;i++)
        {
            data[i]=var;
        }
        last=&data[num-1];
        tail=&data[num-1]+1;
    }
    my_vector(const my_vector &other)       //拷贝构造
    {
        int cap=other.tail-other.data;
        this->num=other.num;
        this->data=new type[cap];
        memcpy(this->data,other.data,cap*sizeof(type));
        this->last=&this->data[this->num-1];
        this->tail=&this->data[cap];
    }
    ~my_vector()        //析构函数
    {
        if(data!=NULL)
        {
            delete []data;
            data=NULL;
        }
        if(last!=NULL)
        {
            delete last;
            last=NULL;
        }
        if(tail!=NULL)
        {
            delete tail;
            tail=NULL;
        }
    }
    my_vector(type *start,type *end)    //用一个容器起始地址到终止地址的元素来初始化vector
    {
        num=end-start;
        data=new type[num*sizeof(type)];
        last=data;
        while(start!=end)
        {
            *last++=*start++;
        }
        tail=last;
    }
    int size(){return num;}     //求vector存储的元素个数
    int capacity(){return tail-data;}      //求vector的最大容量
    void push_back(const type &var)    //在vector末尾添加元素
    {
        if(data==NULL)
        {
            num++;
            data=new type[num];
            data[num-1]=var;
            last=data;
            tail=data+1;
        }
        else
        {
            if((last+1)==tail)
            {
                type *temp=new type[num*2];
                memcpy(temp,data,num*sizeof(type));
                delete []data;
                data=temp;
                data[num]=var;
                last=&data[num];
                tail=&data[num*2];
                num++;
            }
            else
            {
                data[num]=var;
                last++;
                num++;
            }
        }
    }
    void pop_back()     //从vector末尾删除元素
    {
        if(num==0){
            last=data;
        }else{
            last--;
            num--;
        }
    }
    type &at(int pos)       //访问vector指定位置的元素
    {
        if(pos>num)
            throw string("error");
        return data[pos];
    }
    type &front(){return *data;}    //访问vector第一个元素
    type &back(){return *last;}     //访问vector最后一个元素
    type *begin(){return data;}     //返回vector第一个元素的地址
    type *end(){return last+1;}     //返回vector最后一个元素的下一个地址
    void clear()        //清除vector所有元素
    {
        int cap=tail-data;
        type *temp=new type[cap];
        delete []data;
        data=temp;
        last=data;
        tail=&data[cap];
        num=0;
    }
    bool empty(){return num==0?1:0;}    //判断vector中是否有元素
};


int main()
{
    my_vector<int> v1;      //定义整形v1调用无参构造
    for(int i=1;i<=10;i++)      //往vector中添加元素
    {
        v1.push_back(i);
    }
    //求vector中所有元素的个数以及vector的容量
    cout<<"v1.size:"<<v1.size()<<"  v1.capacity:"<<v1.capacity()<<endl;
    cout<<"v1.front:"<<v1.front()<<endl;    //访问vector第一个元素
    cout<<"v1.back:"<<v1.back()<<endl;      //访问vector最后一个元素
    for(int i=0;i<10;i++)
    {
        try {
           cout<<v1.at(i)<<" ";     //访问vector中所有元素
        } catch (string e) {
            cout<<e;
        }
    }
    cout<<endl;
    cout<<"--------------------------------------------"<<endl;
    my_vector<char> v2(5,'a');      //定义字符型v2调用有参构造
    cout<<"v2.size:"<<v2.size()<<"  v2.capacity:"<<v2.capacity()<<endl;
    v2.push_back('w');
    for(int i=0;i<6;i++)
    {
        try {
           cout<<v2.at(i)<<" ";
        } catch (string e) {
            cout<<e;
        }
    }
    cout<<endl;
    v2.clear();     //清空vector中所有元素
    cout<<"v2.empty:"<<v2.empty()<<endl;        //判断vector是否为空
    cout<<"v2.size:"<<v2.size()<<"  v2.capacity:"<<v2.capacity()<<endl;
    cout<<"--------------------------------------------"<<endl;
    int arr[]={5,2,8,5,0,1,6,0,3};
    my_vector<int> v3(arr,arr+7);       //定义整形v3用一个整型数组初始化
    for(int i=0;i<7;i++)
    {
        try {
           cout<<v3.at(i)<<" ";
        } catch (string e) {
            cout<<e;
        }
    }
    cout<<endl;
    cout<<"v3.size:"<<v3.size()<<"  v3.capacity:"<<v3.capacity()<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值