类模板案例
案例描述: 实现一个通用的数组类,要求如下:
可以对内置数据类型以及自定义数据类型的数据进行存储
将数组中的数据存储到堆区
构造函数中可以传入数组的容量
提供对应的拷贝构造函数以及operator=防止浅拷贝问题
提供尾插法和尾删法对数组中的数据进行增加和删除
可以通过下标的方式访问数组中的元素
可以获取数组中当前元素个数和数组的容量
.hpp文件
#include <iostream> #include <string> using namespace std; /* 案例描述: 实现一个通用的数组类,要求如下:* 可以对内置数据类型以及自定义数据类型的数据进行存储 * 将数组中的数据存储到堆区 * 构造函数中可以传入数组的容量 * 提供对应的拷贝构造函数以及operator=防止浅拷贝问题 * 提供尾插法和尾删法对数组中的数据进行增加和删除 * 可以通过下标的方式访问数组中的元素 * 可以获取数组中当前元素个数和数组的容量*//* 思路:: 通用数组类的数组由 数组的数据存储指针、数据大小、数组的容量做限制 */template<class T> class MyArray { public://构造函数MyArray(int capacity){this->m_Capacity = capacity;this->m_Size = 0;pAddress = new T[this->m_Capacity]; //初始化}//拷贝构造 MyArray(const MyArray& arr) {this->m_Capacity = arr.m_Capacity;this->m_Size = arr.m_Size;this->pAddress = new T[this->m_Capacity];for (int i = 0; i < this->m_Size; i++){//如果T为对象,而且还包含指针,必须需要重载 = 操作符 , 因为这个等号不是 构造 而是赋值// 普通类型可以直接 = 但是指针需要进行深拷贝this->pAddress[i] = arr.pAddress[i];}}//重载 = 操作符 防止浅拷贝问题MyArray& operator =(const MyArray& myarray) {// 1. 自赋值检查if (this == &myarray) {return *this;}// 2. 先释放原有的堆空间,避免内存泄漏。if (this->pAddress != NULL) {delete[] this->pAddress;this->m_Capacity = 0;this->m_Size = 0;}// 3. 深拷贝数据this->m_Capacity = myarray.m_Capacity;this->m_Size = myarray.m_Size;this->pAddress = new T[this->m_Capacity];for (int i = 0; i < this->m_Size; i++){this->pAddress[i] = myarray[i];}// 4. 返回当前对象自身return *this;}//重载[] 操作符 arr [0]T& operator[](int index) {return this->pAddress[index]; // 此处不考虑越界,用户自己处理}//尾插法 在数组尾部添加值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 || this->m_Capacity == 0){return;}this->m_Size--;}//获取数组容量int get_Capacity() {return this->m_Capacity;}//获取数组大小int get_Size() {return this->m_Size;}//析构函数~MyArray() {if (this->pAddress != NULL) {delete[] this->pAddress;this->pAddress = NULL;this->m_Capacity = 0;this->m_Size = 0;}}private:T* pAddress; //堆数组指针int m_Capacity;//数组容量int m_Size;//当前元素个数 };
main.cpp
#include <iostream> #include <string> #include "templater_Example.hpp" using namespace std;void printIntArray(MyArray<int>& arr) {for (int i = 0; i < arr.get_Size(); i++){cout << arr[i] << " ";}cout << endl; }//测试内置数据类型void test01() {MyArray<int>array1(10);for (int i = 0; i < 10; i++){array1.push_back(i);}cout << "array1的打印输出: " << endl;printIntArray(array1);cout << "array1的大小: " << array1.get_Size() << endl;cout << "array1的容量: " << array1.get_Capacity() << endl;cout << "---------------------------" << endl;MyArray<int> array2(array1);array2.pop_back();cout << "array2打印输出" << endl;printIntArray(array2);cout << "array2的大小: " << array2.get_Size() << endl;cout << "array2的容量: " << array2.get_Capacity() << endl;}//测试自定义数据类型 class Person { public: Person(){}Person(string name, int age) {this->m_name = name;this->m_age = age;}public:string m_name;int m_age;};void printPersonArray(MyArray<Person>& personArr) {for (int i = 0; i < personArr.get_Size(); i++){cout << "姓名 : " << personArr[i].m_name << "年龄 : " <<personArr[i].m_age << endl;} }void test02() {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);cout << "pArray的大小" << pArray.get_Size() << endl;cout << "pArray的大小" << pArray.get_Capacity() << endl;}int main() {//test01();test02();system("pause");return 0;}