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

#include<iostream>

using namespace std;

#include "MyArray.hpp"

void printArray(MyArray<int> &arr)

{

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

{

cout << arr[i] << " ";

}

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 << arr[i].m_Name << arr[i].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 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++)

{

this->pAddress[i] = arr.pAddress[i];

}

}

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 T[this->m_Capacity];

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

{

this->pAddress[i] = arr.pAddress[i];

}

return *this;

}

void push_back(const T &val)

{

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

{

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

return;

}

this->pAddress[this->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->pAddress[index];

}

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;

};

相关推荐
Charlie_lll6 小时前
力扣解题-移动零
后端·算法·leetcode
chaser&upper6 小时前
矩阵革命:在 AtomGit 解码 CANN ops-nn 如何构建 AIGC 的“线性基石”
程序人生·算法
weixin_499771556 小时前
C++中的组合模式
开发语言·c++·算法
初级代码游戏6 小时前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
_waylau6 小时前
鸿蒙架构师修炼之道-架构师的职责是什么?
开发语言·华为·harmonyos·鸿蒙
2的n次方_6 小时前
CANN Ascend C 编程语言深度解析:异构并行架构、显式存储层级与指令级精细化控制机制
c语言·开发语言·架构
iAkuya7 小时前
(leetcode)力扣100 62N皇后问题 (普通回溯(使用set存储),位运算回溯)
算法·leetcode·职场和发展
近津薪荼7 小时前
dfs专题5——(二叉搜索树中第 K 小的元素)
c++·学习·算法·深度优先
xiaoye-duck7 小时前
吃透 C++ STL list:从基础使用到特性对比,解锁链表容器高效用法
c++·算法·stl
松☆7 小时前
CANN与大模型推理:在边缘端高效运行7B参数语言模型的实践指南
人工智能·算法·语言模型