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;

};

相关推荐
名字还没想好☜16 分钟前
Go 结构体内存对齐:调整字段顺序,同样的字段省下 40% 内存
开发语言·后端·golang·go·内存对齐
玛丽莲茼蒿18 分钟前
很难出错的Spring5(十)—— IOC进阶
java·开发语言·jvm
Ulyanov18 分钟前
雷达导引头仿真中的坐标系——从惯性系到量测角
开发语言·python·算法·系统仿真·雷达信号处理·雷达导引头
玖玥拾22 分钟前
LeetCode 13 罗马数字转整数
笔记·算法·leetcode
m沐沐1 小时前
【计算机视觉】OpenCV 物体跟踪——原理、算法与CSRT跟踪器实战
人工智能·python·深度学习·opencv·算法·计算机视觉·人脸识别
前鼻音太阳熊1 小时前
【MES系统】- 工业HMI状态机可视化实践:从手写SVG到Cytoscape.js的技术选型与踩坑
开发语言·javascript·ecmascript
明日清晨1 小时前
VS2022禁用 NRVO/返回值优化(不调用拷贝构造函数处理方式)
c++
阿米亚波1 小时前
【C++ STL】std::array
java·开发语言·c++
choumin1 小时前
结构型模式——组合模式
c++·设计模式·组合模式·结构型模式
wuyk5551 小时前
66.嵌入式C语言进阶:共用体(Union)实战指南——单片机内存省一半的神级技巧
c语言·开发语言·stm32·单片机·嵌入式硬件