实现一个通用的数组类
1、可以将内置数据类型以及自定义数据类型的数据进行存储
2、将数组中的数据存储到堆区
3、构造函数中可以传入数组容量
4、提供对应的拷贝构造函数以及operator=防止浅拷贝的问题
5、提供尾插法和尾删法对数组中的数据进行增加和删除
6、可以通过下标的方式访问数组中的元素
7、可以获取数组中当前元素个数和数组的容量
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class T>
class Myarray
{
public:
Myarray(int capacity)
{
cout << "拷贝构造函数调用"<< endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[m_Capacity];
}
//有数据储存在堆区,因此需要自己构建拷贝构造函数和operator=
Myarray(const Myarray &arr)
{
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = new.pAddress//系统默认浅拷贝操作
T* pAddress = new T[arr.m_Capacity];
//将arr中的数据都拷贝到新的地方
for(int i = 0 ; i < m_Size ; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//接下来重载operator=操作符
T& operator=(Myarray &arr)
{
if(this-> pAddress != NULL)
{
detele[] this-> pAddress;
this-> pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
T* pAddress = new T[arr.m_Capacity];
for(int i = 0 ; i < m_Size ; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//尾插法 这个没有印象需要怎么做了,这部分写错了
void Push_Back(int id)
{
if(this->m_Size == this->m_Capacity)
{
return;
}
this->m_Size ++ ;
}
//尾删法
void Pop_Back()
{
this->m_Size--;
}
//通过下标索引访问数组中元素,重构[]
T& operator[](int i)
{
return this->pAddress[i];
}
int getCapacity()
{
return this->m_Capacity;
}
int getSize()
{
return this->m_Size;
}
~Myarray()
{
if(this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Size = 0;
this->m_Capacity = 0 ;
}
}
private:
T *pAddress;
int m_Capacity;
int m_Size;
};
上述代码中 尾插法的代码有问题,在自己实现的时候忘记其本质,下面是尾插法的相关代码
void Push_Back(const T&val) //传递一个类型为 T 的常量引用,函数内部不能修改 val 的值。
{
//判断容量是否等于大小
if (this->m_Capacity == this->m_Size)
{
return;
}
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
#include<iostream>
#include<string>
using namespace std;
#include"Myarray.hpp"
void printinfo(Myarray<int> &arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << endl;
}
}
void test01()
{
Myarray<int> arr1(5);
//arr1[0];//访问不了,所以要重载[]
for (int i = 0; i < 5; i++)
{
arr1.Push_Back(i);//尾插法
}
arr1.Pop_Back();
cout << "arr1的打印输出为" << endl;
printinfo(arr1);
cout << "arr1的大小:" << arr1.getSize() <<
" arr1的容量:" << arr1.getCapacity() << endl;
/*Myarray<int> arr2(arr1);
Myarray<int> arr3(100);
arr3 = arr1;*/
Myarray<int> arr2(arr1);
Myarray<int> arr3(100);
arr2 = arr3;
for (int i = 0; i < arr2.getCapacity(); i++)
{
arr2.Push_Back(i);//尾插法
}
arr2.Pop_Back();
cout << "arr2的打印输出为" << endl;
printinfo(arr2);
cout << "arr2的大小:" << arr2.getSize() <<
" arr2的容量:" << arr2.getCapacity() << 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 << "name = " << arr[i].m_name <<
" age = " << arr[i].m_age << endl;
}
}
void test02()
{
Myarray<Person>arr(10);
Person p1("Tom", 19);
Person p2("Jerry", 20);
Person p3("Sam", 29);
Person p4("Amy", 14);
Person p5("daming", 16);
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
printPersonArray(arr);
cout <<"arr的容量:" << arr.getCapacity() << endl;
cout << "arr的大小:" << arr.getSize() << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
这个案例的实现算是其中最容易理解的一个了,可以手敲