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;

};

相关推荐
IvanCodes2 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
aramae4 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
荆棘鸟智能4 小时前
城市感知设备怎么统一接入?从多协议网关到设备模型的中间件架构设计
人工智能·算法·边缘计算
qq_2518364576 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
郑州光合科技余经理7 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
知识分享小能手7 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
qq_199886877 小时前
第8板块·第2节:统一内存的高级特性与性能调优
c++·人工智能·gpu算力·cuda
+VX:Fegn08958 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
阿里嘎多学长8 小时前
2026-09-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
INGNIGHT8 小时前
270 · 电话号码的字母组合II(Trie)
linux·算法