C++类模板 案例(多回顾)
案例:通用数组类
1.可以对内置数据类型以及自定义数据类型的数据进行存储
2.将数组的数据存储到堆区
3.构造函数中可以传入数组的容量
4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题
5.提供尾插法和尾删法对数组中的数据进行增加和删除
6.可以通过下标的方式访问数组的元素
7.可以获取数组中当前元素个数和数组的容量
MyArrary.hpp 文件
1 //自己通用的数组类 2 #pragma once 3 #include <iostream> 4 using namespace std; 5 template<class T> 6 class MyArray 7 { 8 public: 9 //有参构造 参数 容量 10 MyArray(int capacity) 11 { 12 //cout << "MyArray有参构造调用" << endl; 13 this->m_Capacity = capacity; 14 this->m_Size = 0; 15 this->pAddress = new T[this->m_Capacity]; 16 } 17 //拷贝构造 18 MyArray(const MyArray& arr) 19 { 20 //cout << "MyArray拷贝构造调用" << endl; 21 this->m_Capacity = arr.m_Capacity; 22 this->m_Size = arr.m_Size; 23 //this->pAddress = arr.pAddress; 24 //深拷贝 25 this->pAddress = new T[arr.m_Capacity]; 26 //将arr中的数据都拷贝过来 27 for (int i = 0; i < this->m_Size; i++) 28 { 29 this->pAddress[i] = arr.pAddress[i]; 30 } 31 } 32 //operator= 防止浅拷贝问题 33 MyArray &operator=(const MyArray& arr) 34 { 35 //cout << "MyArray的operator=调用" << endl; 36 //先判断原来堆区是否有数据 如果有 先释放 37 if (this->pAddress != NULL) 38 { 39 delete[] this->pAddress; 40 this->pAddress = NULL; 41 this->m_Capacity = 0; 42 this->m_Size = 0; 43 } 44 //深拷贝 45 this->m_Capacity = arr.m_Capacity; 46 this->m_Size = arr.m_Size; 47 this->pAddress = new T[arr.m_Capacity]; 48 for (int i = 0; i < this->m_Size; i++) 49 { 50 this->pAddress[i] = arr.pAddress[i]; 51 } 52 return *this; 53 } 54 //尾插法 55 void Push_Back(const T& val) 56 { 57 //判断容量是否等于大小 58 if (this->m_Capacity == this->m_Size) 59 { 60 return; 61 } 62 this->pAddress[this->m_Size] = val;//在数组末尾插入数据 63 this->m_Size++;//更新数组大小 64 } 65 //尾删法 66 void Pop_Back() 67 { 68 //让用户访问不到最后一个元素,即为尾删,逻辑删除 69 if (this->m_Size == 0) 70 { 71 return; 72 } 73 this->m_Size--; 74 } 75 //通过下标方式访问数组中元素 arr[0] = 100 76 T& operator[](int index) 77 { 78 return this->pAddress[index]; 79 } 80 //返回数组容量 81 int getCapacity() 82 { 83 return this->m_Capacity; 84 } 85 //返回数组大小 86 int getSize() 87 { 88 return this->m_Size; 89 } 90 // 析构函数 91 ~MyArray() 92 { 93 if (this->pAddress != NULL) 94 { 95 //cout << "MyArray析构函数调用" << endl; 96 delete[] this->pAddress; 97 this->pAddress = NULL; 98 } 99 } 100 private: 101 T* pAddress;//指针指向堆区开辟的真实数组 102 int m_Capacity;//数组容量 103 int m_Size;//数组大小 104 };
Main.cpp 文件
1 #include<iostream> 2 using namespace std; 3 #include "MyArray.hpp" 4 5 void printIntArray(MyArray<int>& arr) 6 { 7 for (int i = 0; i < arr.getSize(); i++) 8 { 9 cout << arr[i] << endl; 10 } 11 } 12 void test() 13 { 14 MyArray<int> arr1(5);//有参 析构 15 for (int i = 0; i < 5; i++) 16 { 17 arr1.Push_Back(i); 18 } 19 cout << "arr1的打印" << endl; 20 printIntArray(arr1); 21 cout << "arr1的容量为:" << arr1.getCapacity() << endl;//5 22 cout << "arr1的大小为:" << arr1.getSize() << endl;//5 23 //MyArray<int> arr2(arr1);//有参 拷贝 析构 析构 24 cout << "arr2的打印" << endl; 25 MyArray<int> arr2(arr1); 26 printIntArray(arr2); 27 //尾删 28 cout << "arr2尾删后输出:" << endl; 29 arr2.Pop_Back(); 30 cout << "arr2的容量为:" << arr2.getCapacity() << endl;//5 31 cout << "arr2的大小为:" << arr2.getSize() << endl;//4 32 33 //MyArray<int> arr3(100); 34 //arr3 = arr1;//有参 拷贝 有参 operator 析构 析构 析构 35 } 36 37 //测试自定义数据类型 38 class Person 39 { 40 public: 41 Person() {}; 42 Person(string name, int age) 43 { 44 this->m_Name = name; 45 this->m_Age = age; 46 } 47 string m_Name; 48 int m_Age; 49 }; 50 void printPersonArray(MyArray<Person>& arr) 51 { 52 for (int i = 0; i < arr.getSize(); i++) 53 { 54 cout << "Name:" << arr[i].m_Name << "\tAge:" << arr[i].m_Age << endl; 55 } 56 } 57 void test2() 58 { 59 MyArray<Person> arr(10); 60 Person p1("11", 111); 61 Person p2("22", 222); 62 Person p3("44", 444); 63 Person p4("33", 333); 64 Person p5("55", 555); 65 //将数据插入到数组中 66 arr.Push_Back(p1); 67 arr.Push_Back(p2); 68 arr.Push_Back(p3); 69 arr.Push_Back(p4); 70 arr.Push_Back(p5); 71 //打印数组 72 printPersonArray(arr); 73 cout << "arr容量为:" << arr.getCapacity() << endl; 74 cout << "arr大小:" << arr.getSize() << endl; 75 } 76 int main() 77 { 78 //test(); 79 test2(); 80 system("pause"); 81 return 0; 82 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)