C++基础 -39- 阶段练习 编写通用数组
功能要求
-1- 使用尾插法和尾删法对数组中的数据修改
-2- 构造函数传入数组的大小和容量
-3- 可以通过下标的方式访问数组的中的元素
-4- 可以获取数组中的元素个数和数组的容量

#include <iostream>
using namespace std;
template<class T>
class MyArray
{public://构造函数MyArray(int capacity){pAddress=new T[capacity];this->m_Size=0;this->m_Capacity=capacity;}//拷贝构造MyArray(const MyArray & arr){}//重载= 操作符 防止浅拷贝问题MyArray& operator=(const MyArray&myarray) {}//重载[] 操作符 arr[0]T& operator [](int index){if(index>this->m_Capacity){cout << "数组越界了" << endl;cout << this->pAddress[(this->m_Size)-1] << endl;}return this->pAddress[index];}//尾插法void Push_back(const T & val){this->pAddress[this->m_Size]=val;this->m_Size++;}//尾删法void Pop_back(){this->m_Size--;}//获取数组容量int getCapacity(){return this->m_Capacity;}//获取数组大小int getSize(){return this->m_Size;}//析构~MyArray(){delete []this->pAddress;}private:T * pAddress; //指向一个堆空间,这个空间存储真正的数据int m_Capacity; //容量int m_Size; // 大小
};void show( MyArray<int>& a)
{for(int i=0;i<a.getSize();i++){cout << a[i] << endl;}
}int main()
{MyArray <int> rlxy(1024);rlxy.Push_back(10);rlxy.Push_back(20);rlxy.Push_back(30);rlxy.Push_back(40);show(rlxy);rlxy.Pop_back();show(rlxy);}
未完成