zoukankan      html  css  js  c++  java
  • 自定义数组容器MyArray框架

    #include<iostream>
    
    using namespace std;
    
    template <typename T>
    class  MyArray
    {
    public:
        MyArray(int nCapacity)
        {
            this->mCapacity = nCapacity;
            this->mSize = 0;
            this->pAddr = new T[this->mCapacity];
        }
    
        //拷贝构造
        MyArray(const MyArray<T>& arr)
        {
            this->mCapacity = arr.mCapacity;
            this->mSize = arr.mSize;
            this->pAddr = new T[arr.mCapacity];
            for (size_t i = 0; i < arr.mSize; i++)
            {
                this->pAddr[i] = arr.pAddr[i];
            }
        }
    
        ~MyArray()
        {
            if (this->pAddr != NULL)
            {
                delete this->pAddr;
            }
        
        }
    
        //重载运算符[]
        T& operator[](int nIndex)
        {
            return this->pAddr[nIndex];    //返回数组偏移量为nIndex的引用
        }
    
        //重载运算符=
        MyArray<T> operator=(const MyArray<T>& arr)
        {
            if (this->pAddr != NULL)
            {
                delete this->pAddr;
            }
    
            this->mCapacity = arr.mCapacity;
            this->mSize = arr.mSize;
            this->pAddr = new T[arr.mCapacity];
            for (size_t i = 0; i < arr.mSize; i++)
            {
                this->pAddr[i] = arr.pAddr[i];
            }
            return *this;
        }
        //往数组里添加数据
        void PushBack(T& data)
        {
            //数组当前数据的个数等于或大于数组容量时,这时不能够添加数据
            if (this->mSize >= this->mCapacity)
            {
                return;
            }
            this->pAddr[this->mSize] = data;
            this->mSize++;
        }
        //往数组里添加数据,对右值取引用
        void PushBack(T&& data)
        {
            //数组当前数据的个数等于或大于数组容量时,这时不能够添加数据
            if (this->mSize >= this->mCapacity)
            {
                return;
            }
            this->pAddr[this->mSize] = data;
            this->mSize++;
        }
        
    
    public:
        //当前数组一共可以容下多少个元素
        int mCapacity;
        //当前数组有多少个元素
        int mSize;
        //保存数据的首地址
        T* pAddr;
    
    };
    
    void TestOne()
    {
        MyArray<int> myArray(30);
        int nData1, nData2, nData3;
        nData1 = 10;
        nData2 = 20;
        nData3 = 30;
        myArray.PushBack(nData1);
        myArray.PushBack(nData2);
    
        //当直接PushBack具体的数值时,例如100,就不能用void PushBack(T& data),不能对右值取引用,要用到void PushBack(T&& data),多一个&,表示对右值取引用
        //左值:可以在多行使用
        //右值:临时变量,只能当前使用
        myArray.PushBack(100);
        myArray.PushBack(200);
    
        for (size_t i = 0; i < myArray.mSize; i++)
        {
            cout << myArray[i] << endl;
        }
    }
    
    class Person
    {
    public:
        int nAge;
    };
    
    void TestTwo()
    {
        Person p1;
        p1.nAge = 10;
    
        MyArray<Person> myArray(30);
        myArray.PushBack(p1);
    
        for (size_t i = 0; i < myArray.mSize; i++)
        {
            cout << myArray[i].nAge << endl;
        }
    
    }
    
    int main()
    {
        
        TestOne();
        TestTwo();
    
    
        system("pause");
        return 0;
    }
    111
  • 相关阅读:
    calcite介绍
    kylin介绍
    hbase(三)coprocessor
    流式计算-窗口
    实验室服务器琐事
    流畅的python笔记
    语义分割相关网络简述
    leetcode 696
    树的非递归遍历
    leetcode 665
  • 原文地址:https://www.cnblogs.com/zwj-199306231519/p/12997071.html
Copyright © 2011-2022 走看看