zoukankan      html  css  js  c++  java
  • MyArray框架搭建与实现

    #include<iostream>
    using namespace std;
    
    template<class T>
    class MyArray
    {
    public:
    	//构造函数
    	MyArray<T>(int capacity) {
            this->setmCapacity(capacity);
    		//this->mCapacity = capacity  mCapacity是public才这样用
    		this->mSize = 0;
    		//申请内存
    		this->pAddr = new T[this->mCapacity];
    	}
    
    	//拷贝,const保证不改变原来的 MyArray
    	MyArray<T>(const MyArray<T>& arr) {
    		this->mCapacity = arr.mCapacity;
    		this->mSize = arr.mSize;
    		this->pAddr = new T[this.mCapacity];
            //数据拷贝
    		for (int i = 0; i < this.mSize;i++) {
    			this->pAddr[i] = arr.pAddr[i];
    		}
    	}
    
    
    	//重载 [] 运算符
    	T& operator[](int index) {
    		return this->pAddr[index];
    	}
    
    	//重载 = 运算符
    	MyArray<T>& operator=(const MyArray<T>& arr) {
    
    		if (this->pAddr != NULL) {
    			delete[] this->pAddr;
    		}
    		this->mSize = arr.mSize;
    		this->mCapacity = arr.mCapacity;
    		//申请内存空间
    		this->pAddr = new T[this->mCapacity];
    		//数据拷贝
    		for (int i = 0; i < this->mSize; i++) {
    			this->pAddr[i] = arr.pAddr[i];
    		}
    		return *this;
    	}
    
    	//添加元素
    	void PushBack(T& data) {
    		//判断容器中是否有位置
    		if (this->mSize >= this->mCapacity) {
    			return;
    		}
    
    		//调用拷贝构造 =号操作符
    		//1. 对象元素必须能够被拷贝
    		//2. 容器都是值寓意,而非引用寓意 向容器中放入元素,都是放入的元素的拷贝份
    		//3  如果元素的成员有指针,注意深拷贝和浅拷贝问题
    		this->pAddr[this->mSize] = data;
    		//msize++
    		this->mSize++;	
    	}
        
        
    	/*
        定义第二个pushback()函数的意义在于可以直接传入数据,例如
        arr.pushback(50);
        左值表示可以在多行使用的值
        右值一般表示临时变量,只能在当前行使用
        这里的50是一个右值
        两个&&表示对右值取引用
        */
    
    	void PushBack(T&& data) {
    		//判断容器中是否有位置
    		if (this->mSize >= this->mCapacity) {
    			return;
    		}
    		this->pAddr[this->mSize] = data;
    		//msize++
    		this->mSize++;
    	}
        
        /*
        mCapacity是private
        */
        int getmCapacity(int capacity) {
    		return this->mCapacity;
    	}
    	void setmCapacity(int capacity) {
    		this->mCapacity=capacity;
    	}
    
    
    	//析构函数
    	~MyArray() {
    		if (this->pAddr != NULL) {
    			delete[] this->pAddr;
    		}
    	}
    public:
    	//当前数组有多少元素
    	int mSize;
    	//保存数据的首地址
    	T* pAddr;
    
    private:
    	//一共可以容下多少个元素
    	int mCapacity;
    };
    
    
    void test01() {
    
    	MyArray<int> marray(20);
    	int a = 10, b = 20, c = 30, d = 40;
    	marray.PushBack(a);
    	marray.PushBack(b);
    	marray.PushBack(c);
    	marray.PushBack(d);
    
    	//不能对右值取引用
    	//左值 可以在多行使用
    	//临时变量 只能当前行使用
    	marray.PushBack(100);
    	marray.PushBack(200);
    	marray.PushBack(300);
    
    	for (int i = 0; i < marray.mSize; i++) {
    		cout << marray[i] << " ";
    	}
    	cout << endl;
    }
    
    class Person {};
    void test02() {
    	Person p1, p2;
    	MyArray<Person> arr(10);
    	arr.PushBack(p1);
    	arr.PushBack(p2);
    }
    
    
    int main()
    {
    	test01();
    	test02();
        return 0;
    }
    
  • 相关阅读:
    List<T>的使用
    onclientclick和onclick区别
    IOS学习资料
    DataTable排序的一般方法
    jquery特效
    交叉表、行列转换和交叉查询经典
    sql截取查询
    DelPhi学习网站
    EasyDarwin开源云平台接入海康威视EasyCamera摄像机之快照获取与上传
    EasyDarwin开源云平台接入海康威视EasyCamera摄像机之快照获取与上传
  • 原文地址:https://www.cnblogs.com/gqw-myblogs/p/14238539.html
Copyright © 2011-2022 走看看