类模板案例
案例描述:
实现一个通用的数组类,要求如下:
1.可以对内置数据类型以及自定义数据类型的数据进行存储。
2.将数组中的数据存储到堆区
3.构造函数中可以传入数组的容量
4.提供对应的拷贝构造函数以及 operator= 防止浅拷贝问题
5.提供 尾插法 和 尾删法对数组中的数据进行增加和删除
6.可以通过下标的方式访问数组中的元素
7.可以获取数组中当前元素个数和数组的容量
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()
{
if (this ->pAddress != NULL)
{
//cout << "析构调用" << endl;
delete[] this->pAddress;
this ->pAddress = NULL;
//删除并且置空,避免野指针
}
}
MyArray(const MyArray &arr)
{
//cout << "拷贝构造调用" << endl;
this ->m_Capacity = arr.m_Capacity;
this ->m_Size = arr.m_Size;
//浅拷贝
//this ->pAddress = arr.pAddress;
//深拷贝
this ->pAddress = new T[arr.m_Capacity];
for(int i = 0; i < this->m_Size ; i++)
{
this ->pAddress[i] = arr.pAddress[i];
}
}
//operator= 防止浅拷贝问题
MyArray& operator= (const MyArray& arr)
{
//cout << "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 ;
}
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 getCapacity()
{
return this->m_Capacity;
}
int getSize()
{
return this->m_Size;
}
//返回数组大小
private:
T * pAddress;
int m_Capacity;
int m_Size;
};
Template.cpp
#include <iostream>
#include <string>
using namespace std;
#include "MyArray.hpp"
void printIntArray(MyArray <int>& arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << endl;
}
}
void test01()
{
//对已有的数据类型进行测试
MyArray <int>arr1(5);
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);
printIntArray(arr2);
//尾删法
arr2.Pop_Back();
cout << "arr2尾删后:" << endl;
cout << "arr2的容量:" << arr2.getCapacity() <<endl;
cout << "arr2的大小:" << arr2.getSize() <<endl;
}
class Person
{
public:
Person(){};
Person(string name, int age)
{
this ->m_Name = name;
this ->m_Age = age;
}
string m_Name;
int m_Age;
};
void printPersonArray(MyArray<Person> &arr)
{
for (int i =0; i < arr.getSize() ; i++)
{
cout << "Name :" << arr[i].m_Name << endl;
cout << "Age :" << arr[i].m_Age << endl;
}
}
void test02()
{
MyArray<Person> arr(10);
Person p1("xqj",11);
Person p2("zlz",12);
Person p3("ylb",13);
Person p4("yjs",14);
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
cout << "arr中的内容如下" << endl;
printPersonArray(arr);
//输出容量和大小
cout << "arr的容量:" << arr.getCapacity() <<endl;
cout << "arr的大小:" << arr.getSize() <<endl;
}
int main()
{
test02();
system("pause");
return 0;
}