C++编程案例讲解-使用类模板封装数组类

使用类模板封装数组类


案例描述:实现一个通用的数组类

  • 可以对内置的数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组当中当前元素个数和数组的容量

TList.hpp

#pragma once
#pragma once
#include <iostream>
#include <string>
using namespace std;

template<class T>
class TList
{
private:
	//数组记录信息
	T* array;
	//自定义数组的容量
	int* m_Capactity;
	//当前数组已有的数据数量
	int* m_Size;
public:
	//构造函数,可以传递数组的容量信息
	TList(int capactity)
	{
		if (this->array != NULL)
		{
			//释放原本的信息
			delete[] this->array;
			this->array = NULL;
		}
		//在堆区创建一个可以放置目标容量数据的内存区块
		this->array = new T[capactity];
		//已放置的数据索引设置为0
		this->m_Size = new int(0);
		//初始化容量信息
		this->m_Capactity = new int(capactity);
	}
	//拷贝构造函数
	TList(const TList& list)
	{
		this->array = new T[*list.m_Capactity];
		//值拷贝
		for (int i = 0; i < *list.m_Size; i++)
		{
			this->array[i] = list.array[i];
		}
		this->m_Capactity = new int(*list.m_Capactity);
		this->m_Size = new int(*list.m_Size);
	}
	//operator = 防止浅拷贝问题
	TList& operator=(const TList& list)
	{
		if (this->array != NULL)
		{
			delete[] this->array;
			this->array = NULL;
			this->m_Capactity = 0;
			this->m_Size = 0;
		}
		//深拷贝
		this->array = new T[*list.m_Capactity];
		//值拷贝
		for (int i = 0; i < *list.m_Size; i++)
		{
			this->array[i] = list.array[i];
		}
		this->m_Capactity = new int(*list.m_Capactity);
		this->m_Size = new int(*list.m_Size);
	}
	//重载[]下标访问模式
	T& operator[](int index)
	{
		return this->array[index];
	}
	//析构函数
	~TList()
	{
		if (this->array != NULL)
		{
			//释放原本的信息
			delete[] this->array;
			this->array = NULL;
		}
		if (this->m_Size != NULL)
		{
			//释放原本的信息
			delete this->m_Size;
			this->m_Size = NULL;
		}
		if (this->m_Capactity != NULL)
		{
			//释放原本的信息
			delete this->m_Capactity;
			this->m_Capactity = NULL;
		}
	}
	//尾插法
	void add(const T& t)
	{
		this->array[*this->m_Size] = t;
		int size = (*this->m_Size) + 1;
		if (this->m_Size != NULL)
		{
			//释放原本的信息
			delete this->m_Size;
			this->m_Size = NULL;
		}
		this->m_Size = new int(size);
	}
	//尾删法
	void remove()
	{
		if (this->m_Size == 0)
		{
			return;
		}
		this->m_Size--;
	}
	//获取数组的容量
	int getCapactit()
	{
		return *this->m_Capactity;
	}
	//获取数组的容量
	int size()
	{
		return *this->m_Size;
	}
};

调用案例:
main.cpp

#include <iostream>
#include <string>
#include "TList.hpp"

int main()
{
    TList<string> list_1(100);
    TList<string> list_2(list_1);
    list_1.add("string");
    std::cout << " list_1[0] = " << list_1[0] << std::endl;
    std::cout << "size = " << list_1.size() << std::endl;
}

运行结果:
运行结果
上述案例还可以进行扩展,包括但不限于删除某下标,数组排序等相关功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dp_shiyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值