c++day7

仿照vector手动实现自己的myVector,最主要实现二倍扩容功能

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

template <typename T>
class myvector
{
private:
    T *first;   // 指向数组的首地址
    T *last;    // 指向数组中最后一个元素的下一个位置
    T *end;     // 指向数组分配内存的末尾位置
public:
    //无参构造
    myvector<T>(){
        first = new T[1]; // 分配一个长度为1的数组
        last = first;   // 初始时,首尾指针相同
        end = first+1;  // 初始时,末尾指针指向下一个位置
        cout<<"无参构造"<<endl;
    }
    //有参构造,创建指定长度并初始化为指定值的数组
    myvector(int num,const T &val)
    {
        first = new T[num + 1];// 分配长度为num+1的数组
        last = first;// 初始时,首尾指针相同
        end = first+num;// 末尾指针指向数组末尾的下一个位置
        for(int i = 0; i < num; ++i)
        {
            first[i] = val;// 将数组中的每个元素初始化为val
            last++;
        }
        cout<<"有参构造"<<endl;
    }
    //拷贝构造
    myvector(const myvector<T> *other){
       this->first = new T[other->end - other->first +1];
       this->last = other->last;
        this->end = other->end;
        for(int i = 0;i < other->end - other->first ;i++){
            this->first[i] = other->first[i];
            last+=1;
        }
        end+=(other->end-other->first);

    }
    //拷贝赋值
    myvector &operator=(const myvector<T> *other)
    {
        if(this!=&other)
        {
            delete [] first;
            this->first = new T[other->end - other->first +1];
            this->last = other->last;
            this->end = other->end;
            for(int i = 0;i < other->end - other->first ;i++){
            this->first[i] = other->first[i];
        }
    }
             return *this;
    }
    ~myvector(){
        delete [] first;
        first = nullptr;
        last = nullptr;
        end = nullptr;
    }
    //at()函数
    T &at(int pop)
    {
        if(pop < 0 || pop >end - first)
        {
            throw -1;
        }
        return first[pop];
    }
    const T &operator[](const int pos)const
    {
        return first[pos];
    }
    //判空函数
    bool empty()
    {
        if(last ==first)
            return true;
         else
            return false;
    }
    //判满函数
    bool full(){
        return last == end;
    }
    //front函数 返回第一个元素的地址
    T &front(){
        return *first;
    }
    //back函数 返回最后一个元素的地址
    T &back(){
        return *(end - 1);
    }
    //size()函数
     int size() const{
        return last - first;
    }
     //clear函数
     void clear(){
         last = first;
     }
     //expand()二倍扩容
     void expand(){
         int n = last -first;
         T *temp = new T[(end - first)*2];
         for(int i = 0; i < last -first;++i ){
             temp[i]=first[i];
         }
         delete [] first;
         first = temp;
         last = first + n;
         end = first + 2 *(end - first);
         delete []temp;
         temp = nullptr;
     }
     //push_back()将val值插入末尾
     void push_back(const T &val)
     {
         if(full())
             expand();
         *last = val;
         last+=1;
     }

    //尾删
     void pop_back(){
         if(empty())
         {
             throw -1;
         }
         last--;
     }

};

int main()
{
    myvector<int> v1(3,5);
    for(int i = 0;i < v1.size();i++){
        cout<<v1[i]<<" ";
    }
    cout<<endl;
    myvector<int>v2(v1);
    cout<<v2.at(2)<<endl;
    cout<<"v2.size =" <<v2.size()<<endl;
    myvector<int> v3(v2);
   // v3 = v2;

    for(int i = 0;i < v3.size();++i){
        cout<<v3[i]<<" ";
    }
    cout<<endl;
    cout<<v3.size()<<endl;

    cout<<"v2=" <<v2.size()<<endl;
    v3.clear();
    cout<<"v2=" <<v2.size()<<endl;
    cout<<"v3.size = "<<v3.size()<<endl;

    if(v3.empty()){
       cout<<"v3容器为空"<<endl;
    }else {
        cout<<"容器 非空"<<endl;
    }

        v2.push_back(1);
        v2.push_back(6);
        cout<<"v2=" <<v2.size()<<endl;
        for(int i = 0;i <v2.size();++i){
            cout<<v2[i]<<"  ";
        }





    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值