案例描述:
实现一个通用的数组类,要求如下:
·可以对内置数据类型以及自定义数据类型的数据进行存储;
·将数组中的数据存储到堆区;
·构造函数中可以传入数组的容量;
·提供对应的拷贝构造函数以及operator=防止浅拷贝问题;
·提供尾插法和尾删法对数组中的数据进行增加和删除;
·可以通过下标的方式访问数组中的元素;
·可以获取数组中当前元素个数和数组的容量;

MyArray.hpp
实现
·可以对内置数据类型以及自定义数据类型的数据进行存储;
·将数组中的数据存储到堆区;
·构造函数中可以传入数组的容量;
·提供对应的拷贝构造函数以及operator=防止浅拷贝问题;
//自己的通用数组类
#pragma once
#include
using namespace std;
template
class MyArray {
public:
//有参构造
MyArray(int capacity) {
cout << "MyArray有参构造" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
//拷贝构造
MyArray(const MyArray& arr) {
cout << "MyArray拷贝构造" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//深拷贝
this->pAddress = new T[arr.m_Capacity];
//将arr中数据拷贝过来
for (int i = 0; i < arr.m_Size; i++)
this->pAddress[i] = arr.pAddress[i];
}
//重载赋值运算符 operator= 防止浅拷贝问题
MyArray& operator=(const MyArray& arr) {
//先判断原来堆区是否有数据,如有先释放掉
cout << "MyArray重载赋值运算符operator= " << endl;
if (this->pAddress != NULL) {
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
//深拷贝
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[arr.m_Capacity];
for (int i = 0; i < arr.m_Size; i++)
this->pAddress[i] = arr.pAddress[i];
return *this;
}
//析构函数
~MyArray() {
if (this->pAddress != NULL) {
cout << "MyArray析构函数" << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
T* pAddress;//指针指向堆区开辟的真实数组
int m_Capacity;//数组容量
int m_Size;//数组大小
};
#include"MyArray.hpp"
int main() {
MyArrayarr1(100);
MyArrayarr2(arr1);
MyArrayarr3(300);
arr3 = arr2;
}

添加
·提供尾插法和尾删法对数组中的数据进行增加和删除;
·可以通过下标的方式访问数组中的元素;
·可以获取数组中当前元素个数和数组的容量;
MyArray.hpp中增加了插入、删除、查找、容量、大小函数
//尾插法
void Push_Back(const T& val) {
//判断容量是否等于大小
if (this->m_Capacity == this->m_Size) {
cout << "容量已满,无法插入" << endl;
return;
}
this->pAddress[this->m_Size] = val;//在数组末尾插入数据
this->m_Size++;//更新数组大小
}
//尾删法
void Pop_Back() {
//让用户访问不到最后一个元素,即为尾删,逻辑删除
this->m_Size--; //数组大小减1,就访问不到最后一个了
}
//下标访问数据,,,,中括号重载
T& operator[](int index) {
return this->pAddress[index];
}
//返回数组容量
int getCapacity() {
return this->m_Capacity;
}
//返回数组大小
int getSize(){
return this->m_Size;
}
//测试内置数据类型 int
#include"MyArray.hpp"
//测试内置数据类型 int
void printIntArray(MyArray& arr) {
for (int i = 0; i < arr.getSize(); i++)
cout << arr[i] << endl;
}
void test1() {
MyArrayarr1(100);
MyArrayarr2(arr1);
MyArrayarr3(arr2);//拷贝构造
arr3 = arr1;//拷贝构造
//尾插法测试
for (int i = 0; i < 5; i++)
arr1.Push_Back(i);
cout << "\n arr1的容量为:" << arr1.getCapacity() << endl;
cout << "arr1的大小为:" << arr1.getSize() << endl;
cout << "arr1打印输出:" << endl;
printIntArray(arr1);
//尾删法测试
arr1.Pop_Back();
cout << "arr1删除尾元素后容量为:" << arr1.getCapacity() << endl;
cout << "arr1删除尾元素后大小为:" << arr1.getSize() << endl;
cout << "arr1删除尾元素后打印输出:" << endl;
printIntArray(arr1);
}
int main() {
test1();
return 0;
}

//测试自定义数据类型 Person
//测试自定义数据类型 Person
#include"MyArray.hpp"
class Person {
public:
Person() {};
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
int m_age;
string m_name;
};
void printPersonArray(MyArray& arr) {
for (int i = 0; i < arr.getSize(); i++)
cout << "姓名:"<arr(10);
Person p1("悟空",999);
Person p2("妲己", 120);
Person p3(p1);//拷贝构造
//尾插法测试
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p2);
cout << "arr的容量为:" << arr.getCapacity() << endl;
cout << "arr的大小为:" << arr.getSize() << endl;
cout << "arr打印输出:" << endl;
printPersonArray(arr);
//尾删法测试
arr.Pop_Back();
cout << "\narr删除尾元素后容量为:" << arr.getCapacity() << endl;
cout << "arr删除尾元素后大小为:" << arr.getSize() << endl;
cout << "arr删除尾元素后打印输出:" << endl;
printPersonArray(arr);
}
int main() {
test2();
return 0;
}
