c++ 利用模板创建一个可以储存任意类型数据的数组类

#include<iostream>

using namespace std;

#include "MyArray.hpp"

void printArray(MyArray<int> &arr)

{

for (int i = 0; i < arr.getSize(); i++)

{

cout << arri << " ";

}

cout << endl;

}

void test01()

{

MyArray <int>arrl(5);

for(int i=0;i<5;i++)

{

arrl.push_back(i);

}

printArray(arrl);

cout<<"容量为:"<<arrl.getCapacity()<<endl;

cout<<"大小为:"<<arrl.getSize()<<endl;

arrl.pop_back();

printArray(arrl);

}

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 << arri.m_Name << arri.m_Age << endl;

}

}

void test02()

{

MyArray <Person>arr(5);

Person p1("Tom",10);

Person p2("Jerry",11);

Person p3("Mike",12);

Person p4("Lucy",13);

Person p5("Lili",14);

arr.push_back(p1);

arr.push_back(p2);

arr.push_back(p3);

arr.push_back(p4);

arr.push_back(p5);

printPersonArray(arr);

cout<<"容量为:"<<arr.getCapacity()<<endl;

cout<<"大小为:"<<arr.getSize()<<endl;

arr.pop_back();

}

int main()

{

test01();

test02();

cout << "Hello World!" << endl;

system("pause");

return 0;

}

MyArray.hpp

#pragma once

#include <iostream>

using namespace std;

template <class T>

class MyArray

{

public:

MyArray(int capacity )

{

this->m_Capacity = capacity;

this->m_Size = 0;

this->pAddress = new Tthis-\>m_Capacity;

}

MyArray(const MyArray &arr)

{

this->m_Capacity = arr.m_Capacity;

this->m_Size = arr.m_Size;

this->pAddress = new Tthis-\>m_Capacity;

for (int i = 0; i < this->m_Size; i++)

{

this->pAddressi = arr.pAddressi;

}

}

MyArray &operator=(const MyArray &arr)

{

if (this->pAddress != nullptr)

{

delete\[\] this->pAddress;

this->pAddress = nullptr;

this->m_Size = 0;

this->m_Capacity = 0;

}

this->m_Capacity = arr.m_Capacity;

this->m_Size = arr.m_Size;

this->pAddress = new Tthis-\>m_Capacity;

for (int i = 0; i < this->m_Size; i++)

{

this->pAddressi = arr.pAddressi;

}

return *this;

}

void push_back(const T &val)

{

if (this->m_Size == this->m_Capacity)

{

cout << "数组已满" << endl;

return;

}

this->pAddressthis-\>m_Size = val;

this->m_Size++;

}

void pop_back()

{

if (this->m_Size == 0)

{

cout << "数组已空" << endl;

return;

}

this->m_Size--;

}

T &operator\[\](int index)

{

if (index >= this->m_Size || index < 0)

{

cout << "数组越界" << endl;

throw - 1;

}

return this->pAddressindex;

}

int getSize()

{

return this->m_Size;

}

int getCapacity()

{

return this->m_Capacity;

}

~MyArray()

{

if (this->pAddress != nullptr)

{

delete\[\] this->pAddress;

this->pAddress = nullptr;

}

}

private:

T *pAddress;

int m_Capacity;

int m_Size;

};

相关推荐
汉克老师3 分钟前
GESP2026年6月认证C++七级( 第一部分选择题(8-15))精讲
c++·dfs·bfs·哈希·二分算法·gesp7级·网格dp
布朗克1686 分钟前
Go 入门到精通-09-复合类型之Map
开发语言·后端·golang·map
zh_xuan7 分钟前
c++ Expected用法
开发语言·c++·expected
ch.ju8 分钟前
Java程序设计(第3版)第四章——final修饰的方法和类
java·开发语言
小O的算法实验室8 分钟前
2026年ACM TCH,动态救护车路径优化:结合 K-means 聚类与多目标粒子群算法两阶段策略
算法
在书中成长12 分钟前
HarmonyOS 小游戏《对战五子棋》开发第8篇 - GomokuEngine核心引擎设计(三):五子连珠判定算法
算法·华为·harmonyos
Let's Chat Coding25 分钟前
MAC 消息认证码:同时验证来源和完整性
算法·哈希算法
极***技27 分钟前
2026云原生核心!Go高并发限流实战,从原理到落地
开发语言·云原生·golang
牛大了20231 小时前
场景题:数据迁移解决方案
开发语言
cc.ChenLy3 小时前
算法从入门到精通实战指南
算法