MyArray.h
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray {
private:
T* pAddr;
int m_Capacity;
int m_Size;
public:
MyArray(int capacity) {
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddr = new T[this->m_Capacity];
}
MyArray(const MyArray& arr) {
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddr = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++) {
this->pAddr[i] = arr.pAddr[i];
}
}
MyArray& operator=(const MyArray& arr) {
if (this->pAddr != NULL) {
delete[] this->pAddr;
this->pAddr = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddr = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++) {
this->pAddr[i] = arr.pAddr[i];
}
return *this;
}
void Push_Back(const T& val) {
if (this->m_Capacity == this->m_Size) {
cout << "数组已经满了" << endl;
return;
}
this->pAddr[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) {
return this->pAddr[index];
}
int GetCapacity() {
return this->m_Capacity;
}
int GetSize() {
return this->m_Size;
}
~MyArray() {
if (this->pAddr != NULL) {
delete[] this->pAddr;
this->pAddr = NULL;
}
}
};
数组类模板.cpp
#include <iostream>
using namespace std;
#include <string>
#include "MyArray.h"
void printArray(MyArray<int>& arr) {
for (int i = 0; i < arr.GetSize(); i++) {
cout << arr[i] << endl;
}
}
void test01() {
MyArray<int>arr(5);
for (int i = 0; i < 5; i++) {
arr.Push_Back(i);
}
cout << "arr的打印输出为:" << endl;
printArray(arr);
cout << "arr的容量:" << arr.GetCapacity() << endl;
cout << "arr的大小:" << arr.GetSize() << endl;
MyArray <int>arr1(arr);
printArray(arr1);
arr1.Pop_Back();
cout << "arr1尾删后" << endl;
cout << "arr1的容量:" << arr1.GetCapacity() << endl;
cout << "arr1的大小:" << arr1.GetSize() << endl;
}
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 << "\t年龄:" << arr[i].m_age << endl;
}
}
void test02() {
MyArray<Person>arr(10);
Person p1("孙悟空", 555);
Person p2("红孩儿", 555);
Person p3("猪八戒", 555);
Person p4("唐三藏", 555);
Person p5("沙悟净", 555);
Person p6("白骨精", 555);
Person p7("女儿国", 555);
Person p8("白龙马", 555);
Person p9("史蒂夫", 555);
Person p10("甘道夫", 555);
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
arr.Push_Back(p6);
arr.Push_Back(p7);
arr.Push_Back(p8);
arr.Push_Back(p9);
arr.Push_Back(p10);
PrintPersonArray(arr);
cout << "arr的容量:" << arr.GetCapacity() << endl;
cout << "arr的大小:" << arr.GetSize() << endl;
}
int main() {
test02();
system("pause");
return 0;
}