案例描述: 实现一个通用的数组类
- 可以对内置数据类型以及自定义数据类型的数据进行存储
- 将数组中的数据存储到堆区
- 构造函数中可以传入数组的容量
- 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
- 提供尾插法和尾删法对数组中的数据进行增加和删除
- 可以通过下标的方式访问数组中的元素
- 可以获取数组中当前元素个数和数组的容量
说明:首先写一个myArray.hpp代码,用于创建通用的数组。
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//构造函数 参数、容量
MyArray(int capacity)
{
//cout << "正在调用有参构造" << endl;
this->m_capacity = capacity;
this->m_size = 0;
//重新在堆区申请空间
this->pAddress = new T[this->m_capacity];
}
//防止浅拷贝,写一个拷贝构造函数
MyArray(const MyArray &p)
{
//cout << "正在拷贝构造" << endl;
this->m_capacity = p.m_capacity;
this->m_size = p.m_size;
//深拷贝
this->pAddress = new T[p.m_capacity];;
//将p中的数据拷贝过来
for (int i = 0; i < p.m_size; i++)
this->pAddress[i] = p.pAddress[i];
}
//防止浅拷贝另一个 : operator=
MyArray& operator=(const MyArray & p)
{
//cout << "正在operate=构造" << endl;
//先判断原来堆区是否有数据,如果有,先释放
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->m_capacity = 0;
this->m_size = 0;
this->pAddress = NULL;
}
//做深拷贝
this->m_capacity = p.m_capacity;;
this->m_size = p.m_size;
this->pAddress = new T[p.m_capacity];
//把原来数据传入尽量
for (int i = 0; i < p.m_size; i++)
this->pAddress[i] = p.pAddress[i];
return *this;
}
//写一个尾插法
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()
{
if (this->m_size == 0)
{
cout << "容器为空,不能再删除了" << endl;
return;
}
this->m_size--;
}
//写一个运算符重载:通过下标的方式访问数组中的元素
T & operator[](int index)
{
return this->pAddress[index];
}
//返回数组的容量
int getCapacity()
{
return this->m_capacity;
}
//返回数组的大小
int getSize()
{
return this->m_size;
}
//析构函数
~MyArray()
{
if (this->pAddress != NULL)
{
//cout << "正在调用析构函数" << endl;
delete[] this->pAddress;
this->pAddress = NULL; //置空,防止野指针
}
}
private:
int m_size;
int m_capacity;
T * pAddress;
};
测试效果:
void showPrint(MyArray<int> &p)
{
for (int i = 0; i < p.getSize(); i++)
cout << p[i] << " ";
cout << endl;
}
void test()
{
MyArray<int> p(5);
/*MyArray<int>p1(p);
MyArray<int> p2(100);
p2 = p1;*/
for (int i = 0; i < 5; i++)
{
p.Push_back(i+1);
}
cout << "打印数组中的数据:" << endl;
showPrint(p);
cout << "数组的容量为:" << p.getCapacity()<< endl;
cout << "数组的大小为:" << p.getSize() << endl;
cout << "删除尾元素:" << endl;
p.Pop_back();
cout << "删除后,打印数组中的数据:" << endl;
showPrint(p);
cout << "删除后,数组的容量为:" << p.getCapacity() << endl;
cout << "删除后,数组的大小为:" << p.getSize() << endl;
}
举例子测试:
void printPersonArray(MyArray<Person> &personArr)
{
for (int i = 0; i < personArr.getSize(); i++) {
cout << "姓名:" << personArr[i].m_Name << " 年龄: " << personArr[i].m_Age << endl;
}
}
void test01()
{
//创建数组
MyArray<Person> pArray(10);
Person p1("孙悟空", 30);
Person p2("韩信", 20);
Person p3("妲己", 18);
Person p4("王昭君", 15);
Person p5("赵云", 24);
//插入数据
pArray.Push_back(p1);
pArray.Push_back(p2);
pArray.Push_back(p3);
pArray.Push_back(p4);
pArray.Push_back(p5);
printPersonArray(pArray);
cout << "pArray的大小:" << pArray.getSize() << endl;
cout << "pArray的容量:" << pArray.getCapacity() << endl;
}