上一篇介绍了利用模板类实现自定义数组类的一些方法,包括有参构造和拷贝构造和赋值=号函数,还有析构函数。这篇还继续实现后续需求。有尾部插入元素和尾部删除元素,还有根据索引获取元素,以及获取数组容量和获取数组元素大小的方法。
1.剩余代码
接着上一篇,在析构函数代码前面,写尾插法和尾删法,还有operator[]根据索引获取元素方法,以及上一篇定义了m_Capactiy和m_Size是私有成员变量,这里我们需要提供get方法让外部使用。
#include <iostream>
#include <string>
using namespace std;
template <class T>
class myArray{
public:
// 有参构造函数
myArray(int capacity)
{
cout << "myArray 的有参构造被调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
// 拷贝构造
myArray(const myArray& arr)
{
cout << "myArray 的有拷贝造被调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this-pAddress = arr.pAddress; // 指针不能这样浅拷贝赋值,会出现堆区数据重复释放
// 这里需要做深拷贝
this->pAddress = new T[arr.m_Capacity];
// 如果数组中还有元素也需要拷贝过来
for(int i=0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
// operate= 重载函数
myArray& operator=(const myArray& arr)
{
cout << "myArray 的有operator=被调用" << endl;
// 先判断堆区是否有数据,如果有先是否
if(this->pAddress != NULL)
{
delete [] 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;
this->pAddress = new T[arr.m_Capacity];
for(int i=0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//尾插法
void PushBack(const T & val)
{
// 先判断是否超过数组容量,超过了不能插入
if (this->m_Capacity == this->m_Size)
{
cout << "数组容量已满,不能尾部插入" << endl;
return;
}
this->pAddress[this->m_Size] = val; // 在数组末尾插入元素
this->m_Size++; // 更新数组大小
}
//尾删法
void PopBack()
{
// 思路就让用户访问不到最后一个元素,这里删除是逻辑删除
if (this->m_Size == 0)
{
cout << "数组已空,不能进行删除" << endl;
return;
}
this->m_Size--;
}
//使用下标访问数组元素
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getCapacity()
{
return this->m_Capacity;
}
//返回数组元素大小
int getSize()
{
return this->m_Size;
}
//析构函数
~myArray()
{
if(this->pAddress != NULL)
{
cout << "myArray 的析构被调用" << endl;
delete [] this->pAddress; // 清空内存
this->pAddress = NULL; // 指针置空,防止野指针
}
}
private:
T * pAddress; // 指针,维护是指向堆区中真实开辟出来数组
int m_Capacity; // 数组的容量
int m_Size; // 数组大小(元素个数)
};
下面开始写一些测试代码进行验证
2.测试代码
前面创建的数组都是空对象,里面没有实际的元素,现在我们先使用尾插法添加两个元素,然后调用根据索引获取元素的值,然后调用获取数组元素个数是不是等于2
#include <iostream>
#include <string>
#include "myArray.hpp"
using namespace std;
void test01()
{
myArray <int>arr1(5); //有参构造
arr1.PushBack(3); // 尾插法
arr1.PushBack(5); // 尾插法
cout << "第一个元素是:" << arr1.operator[](0) << endl;
cout << "第二个元素是:" << arr1.operator[](1) << endl;
cout << "当前数组元素大小是:" << arr1.getSize() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行
接着测试容量大小,和尾删法
#include <iostream>
#include <string>
#include "myArray.hpp"
using namespace std;
void test01()
{
myArray <int>arr1(5); //有参构造
arr1.PushBack(3); // 尾插法
arr1.PushBack(5); // 尾插法
int capacity = arr1.getCapacity(); // 应该是5
cout << "当前数组元素大小是:" << arr1.getSize() << endl;
cout << "当前数组容量是:" << capacity << endl;
arr1.PopBack();
arr1.PopBack();
cout << "当前数组元素大小是:" << arr1.getSize() << endl;
arr1.PopBack(); // 提示为空不能继续删除
}
int main()
{
test01();
system("pause");
return 0;
}
测试结果
接下来学习C++的STL,也就是一些标准类库,例如string类用来处理字符串,还有一些基本容器类vector等。