類模板案例
案例描述: 實現一個通用的數組類,要求如下:
可以對內置數據類型以及自定義數據類型的數據進行存儲
將數組中的數據存儲到堆區
構造函數中可以傳入數組的容量
提供對應的拷貝構造函數以及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;}