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;

};

相关推荐
阿i索几秒前
【C++学习笔记】【基础】4.string类(2)——模拟实现
c++·笔记·学习
天天爱吃肉8218几秒前
豆包 vs DeepSeek API 对比分析报告
android·java·大数据·开发语言·功能测试·嵌入式硬件·汽车
北域码匠11 分钟前
奇偶归并排序:并行计算的排序利器
数据结构·算法·c#·排序算法
geovindu12 分钟前
python: Reactor Pattern
开发语言·python·设计模式·反应器模式
我不是懒洋洋12 分钟前
从零实现一个消息队列:生产消费与持久化
c++
CS_SKILL17 分钟前
吉比特 C++ 实习一面面经:一轮把 C++、容器、并发、排序和网络全扫了一遍
java·开发语言·校招面经·实习面经·技术面经·吉比特校招
feifeigo12318 分钟前
基于多混沌映射的图像加密(MATLAB实现)
开发语言·matlab
成都易yisdong21 分钟前
上海某平面坐标系与CGCS2000坐标互转详解(含全域拟合点、实战案例、保密规范)
大数据·人工智能·算法
techdashen21 分钟前
Go 语言仓库 Top 100 贡献者分析报告
开发语言·后端·golang
何以解忧,唯有..22 分钟前
Go 语言变量命名规范详解
开发语言·后端·golang