案例描述:实现一个通用的数组类,要求如下:
1.可以对内置数据类型以及自定义数据类型进行储存
2.将数组中的数据存储到堆区
3.构造函数中可以传入数组的容量
4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题
5.提供尾插法和尾删法对数组中的数据进行增加和删除
6.可以通过下标的方式访问数组中的元素
7.可以获取数组中当前元素个数和数组的容量
hpp:
//自己的通用的数组类
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//有参构造 参数 容量
MyArray(int capacity)
{
this->m_Capacity = capacity;
this->Size = 0;
this->pAddress = new T[this->Capacity];
}
//拷贝构造函数
MyArray(const MyArray& arr)
{
//cont << "Myarray有参构造调用"<<endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = arr.pAddress;//这是浅拷贝,同时这是指针赋值,会导致堆区数据重复释放
//深拷贝
this->pAddress = new T[arr.Capacity];
//将arr中的数据都拷贝过来
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//operator=防止浅拷贝问题 //符号重载
MyArray& operator=(const MyArray& arr)
{
//cont << "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 < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//尾插法
void Push_Back(const T& val)
{
//判断容量是否等于大小
if (this->m_Capacity == this->m_Size)
{
return;
}//因为数组是从0开始的,所以
this->pAddress[this->m_Size] = val;//在末尾插入数据
this->m_Size++;
}
//尾删法
void Pop_Back()
{
//让用户访问不到最后一个元素,即为尾删,逻辑删除
if (this->m_Size == 0)
{
return;
}
this->m_Size--;
}
//通过下标方式访问数组中的元素
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getcCapacity()
{
return this->m_Capacity;
}
//返回数组大小
int getSize()
{
return this->m_Size;
}
//析构函数
~MyArray()
{
if (this = > pAddress != NULL)
{
//cont << "Myarray析构函数调用" << endl;
delete[]this->pAddress;
this->pAddress = NULL;
}
}
private:
T* pAddress;//指针指向堆区开辟的真实数组
int m_Capacity;//数组容量
int m_Size;//数组大小
};
cpp:
#include<iostream>
using namespace std;
#include"MyArry.hpp"
void PrintIntArray(MyArray<int>& arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << endl;
}
}
void test01()
{
MyArray<int>arr1(5);
arr1[0];//访问不到,因为arr是对象,不是数组
for (int i = 0; i < 5; i++)
{
//利用尾插法向数组中插入数据
arr1.Push_Back(i);
}
cout << "arr1的打印输出为:" << endl;
PrintIntArray(arr1);
cout << "arr1的容量为:" << arr1.getCapacity() << endl;
cout << "arr1的大小为:" << arr1.getSize() << endl;
/*MyArray<int>arr2(arr1);
MyArray<int>arr3(100);
arr3 = arr1;*/
}
//测试自定义数据类型
class Person
{
public:
Person() {};
person(string name, int age)
{
this->m_NAme = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void printPerosnArray(MyArray<Person>& arr)
{
for (int i = 0; i < arr.gerSize(); i++)
{
cout << "姓名: " << arr[i].m_Name << "年龄: " << arr[i].m_Age << endl;
}
}
void test02()
{
MyArray<Person>arr(10);//10是容量
Person p1("孙悟空", 999);
Person p2("韩信", 30);
Person p2("妲己", 20);
Person p2("赵云", 25);
Person p2("安琪拉", 27);
//将数据插入到数组中
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
//打印数组
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
//输出容量
cout << "arr容量为: " <<arr.gerCapacity()<< endl;
//输出大小
cout << "arr大小为: " << arr.gerSize() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:能够利用所学知识实现通用的数组
仅个人看视频笔记与理解,如有误可指出谢谢