要求
- 可以对内置数据类型及自定义数据类型的数据进行存储
- 将数组中的数据存储到堆区
- 构造函数可以传入数组的容量
- 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
- 提供尾插法和尾删除法对数组中的数据解析增加和删除
- 可以通过下标的方式访问数组的元素
- 可以获取数组中当前元素个数和数组的容量
分析
使用模板类
myArray
{
public:
构造函数(容量)
拷贝构造函数
operator=
尾插
尾删
下标访问operator[]
获取容量
获取大小
析构函数
private:
指向数据存储数组的指针
容量
大小
}
实现
#pragma once
#include <iostream>
using namespace std;
//myArray.hpp
template<class T>
class myArray
{
public:
myArray(int capacity); //构造函数
myArray(const myArray &arr); //拷贝构造函数
~myArray(); //析构函数
const myArray& operator=(const myArray &arr); //operator=
void pushBack(const T &val);//尾插
void popBack();//尾删
T& operator[](int index);//下标访问
int getCapacity(); //获取容量
int getSize(); //获取大小
private:
T *pAddress;
int m_Capacity;
int m_Size;
};
template<class T>
myArray<T>::myArray(int capacity) //构造函数
{
//cout << "构造函数" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
pAddress = new T[this->m_Capacity];
}
template<class T>
myArray<T>::myArray(const myArray &arr) //拷贝构造函数
{
//cout << "拷贝构造函数" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
pAddress = new T[this->m_Capacity];
for (int i = 0; i < this->m_Capacity; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
template<class T>
myArray<T>::~myArray() //析构函数
{
//cout << "析构函数" << endl;
if (this->pAddress != NULL)
delete[]this->pAddress;
}
template<class T>
const myArray<T>& myArray<T>::operator=(const myArray &arr) //operator=
{
//cout << "operator=" << endl;
if (this->pAddress != NULL)
{
this->m_Capacity = 0;
this->m_Size = 0;
delete[]this->pAddress;
this->pAddress = 0;
}
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
pAddress = new T[this->m_Capacity];
for (int i = 0; i < this->m_Capacity; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
template<class T>
void myArray<T>::pushBack(const T &val) //尾插
{
if (this->m_Capacity == this->m_Size)
return;
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
template<class T>
void myArray<T>::popBack() //尾删
{
if (this->m_Size == 0)
return;
this->m_Size--;
}
template<class T>
T& myArray<T>::operator[](int index) //下标访问
{
return this->pAddress[index];
}
template<class T>
int myArray<T>::getCapacity() //获取容量
{
return this->m_Capacity;
}
template<class T>
int myArray<T>::getSize() //获取大小
{
return this->m_Size;
}
main文件,测试使用
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#include "myArray.hpp"
void printMyIntArray(myArray<int> &arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << endl;
}
}
void test1()
{
myArray<int> arr1(5);
for (int i = 0; i < 5; i++)
{
arr1.pushBack(i);
}
cout << "arr1:" << endl;
printMyIntArray(arr1);
cout << "capacity" << arr1.getCapacity() << endl;
cout << "size" << arr1.getSize() << endl;
myArray<int> arr2(arr1);
cout << "arr2:" << endl;
printMyIntArray(arr2);
arr2.popBack();
cout << "arr2:" << endl;
printMyIntArray(arr2);
cout << "capacity" << arr1.getCapacity() << endl;
cout << "size" << arr1.getSize() << endl;
myArray<int> arr3(10);
arr3 = arr2;
cout << "arr3:" << endl;
printMyIntArray(arr3);
}
class Person
{
public:
Person(){};
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void printMyPersonArrary(myArray<Person> &array)
{
for (int i = 0; i < array.getSize(); i++)
{
cout << "name:" << array[i].m_Name << " age:" << array[i].m_Age << endl;
}
}
void test2()
{
myArray<Person> arr1(10);
arr1.pushBack(Person("1", 1));
arr1.pushBack(Person("2", 2));
arr1.pushBack(Person("3", 3));
arr1.pushBack(Person("4", 4));
arr1.pushBack(Person("5", 5));
printMyPersonArrary(arr1);
}
void main()
{
//test1();
test2();
system("pause");
}