用模板类实现vector

该文章展示了一个C++实现的模板类Myvector,它模仿了标准库中的vector容器。Myvector包括了基本的元素存储、拷贝构造、析构、赋值操作、容量管理、元素访问以及插入删除功能。文章通过实例展示了如何使用这个自定义向量类进行元素操作,如尾插、查找、扩容、尾删和清空等。

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

#include <iostream>

using namespace std;
template <typename T>
class Myvector
{
private:
    T*first;//向量的第一个元素的指针
    T*last;//向量最后一个元素的指针
    T*end;//指的是分配给向量空间末尾的下一个指针
public:
    Myvector():first(new T[10]),last(first),end(first+10){cout<<"有参构造"<<endl;};//有参构造
    //拷贝构造
    Myvector ( const Myvector &other)
    {
        int size=other.last-other.first;
        first=new T[size];
        for(int i=0;i<size;i++)
        {
            first[i]=other.first[i];
        }
         last=first+size-1;
         end=first+2*size;
         cout<<"拷贝构造"<<endl;
    }
    //析构函数
    ~Myvector(){delete [] first;cout<<"析构函数"<<endl;}
    //拷贝赋值
    Myvector<T>&operator=(const Myvector<T>&other)
    {
      if(this!=&other)
      {
          delete [] first;
          int size=other.size();
          first=new T[size];
          for(int i=0;i<size;i++)
          {
              first[i]=other.first[i];
          }
          last=first+size;
          end=first+2*size;
      }
      return *this;
    }
    //元素个数
    int size() const
    {
        return last - first ;
    }
    //容纳最大元素个数
    int capacity()const
    {
        return end-first;
    }
    //查找任意值
    T&at(int index)const
    {
        if(index<0||index>= size())
            throw std::out_of_range("index out of range");
        return first[index];

    }
    //判空
    bool isempty()const
    {
        return size()==0;
    }
    //判满
    bool isfull()const
    {
        return size()==capacity();
    }
    //返回第一个元素
    T & front()const
    {
        if(isempty())
            throw std::out_of_range("The vector is empty");
        return *first;
    }
    //返回最后一个元素
    T & lack()const
    {
        if(isempty())
            throw std::out_of_range("The vector is empty");
        return *last;
    }
    //清空向量
    void clear()
    {
        delete [] first;
        first=last=end=nullptr;
    }
    //二倍扩容
    void expand()
    {
        int s=size();

        T *new_first=new T[2*s];
        memcpy(new_first,first,sizeof (T)*s);
        delete [] first;
        first=new_first;
        last=first+s;
        end=first+2*s;
    }
    //尾插
    void push_back(const T&other)
    {
        if(isfull())
            throw std::out_of_range("The vector is full");
        *(last++)=other;
    }
    //尾删
    void pop_back()
    {
        if(isempty())
            throw std::out_of_range("The vector is empty");
        --last;
    }

};

int main()
{
    //创建一个新的整形向量
    Myvector<int> v1;
    //尾插
   // cout<<"向量大小:"<<v1.size()<<endl;
    for(int i=1;i<=5;++i)
    {
        v1.push_back(i);
    }
    cout<<"V1 向量实际大小:"<<v1.size()<<endl;
    cout<<"V1 向量容量:"<<v1.capacity()<<endl;
     //拷贝构造
    Myvector<int>v2=v1;
    cout<<" V2 向量实际大小:"<<v2.size()<<endl;
    cout<<"V2 向量容量:"<<v2.capacity()<<endl;
    //二倍扩容
    v1.expand();
    cout<<"扩容 向量实际大小:"<<v1.size()<<endl;
    cout<<"扩容 向量容量:"<<v1.capacity()<<endl;
    //查找第一个元素
    cout<<"查找第一个元素"<<v1.at(1)<<endl;
    //尾删
    v1.pop_back();
    cout<<"尾删 向量实际大小:"<<v1.size()<<endl;
    cout<<"尾删 向量容量:"<<v1.capacity()<<endl;
   //清空向量
    v2.clear();
     cout<<" V2 清空后 向量实际大小:"<<v2.size()<<endl;

    return 0;
}

输出结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值