前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数组类模板(进阶版)

数组类模板(进阶版)

作者头像
用户11039529
发布2024-04-02 08:45:53
640
发布2024-04-02 08:45:53
举报
文章被收录于专栏:算法学习日常算法学习日常

介绍:

简单实现版本在这里:数组类模板(类模拟实现静态数组)(简单版)-CSDN博客

简单版本分析了案例要求怎么实现,对该项目的实现的思路有帮助哦

分析:

* 因为不可能把所有代码放在源文件,所以要创建头文件---->存放类模板的相关信息 * 因为用了类模板,所以在调用时才给类分配内存,因此不可以写成:.h文件中为类模板的声 明,.cpp中为类模板的实现,会报错 创建.hpp文件,存放类模板的声明和实现

实现:

.hpp框架创建

代码语言:javascript
复制
#pragma once/*头文件都要写这个哦,防止被重复包含,重复包含的弊端:编译阶段时编译器会将头文件全部展开放进源文件中,若重复包含,则头文件会被执行两次,占内存且耗时*/

#include <iostream>

using namespace std;

template<typename T>/*类模板*/
class MyArray
{
public:

private:
	T* pAddress;
	int m_Size;
	int m_Capacity;
};

.hpp函数内容

有参构造
代码语言:javascript
复制
	//有参构造
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		
		pAddress = new T[capacity];
	}
拷贝构造:
代码语言:javascript
复制
	//拷贝构造
	MyArray(const MyArray& arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

		//浅拷贝
		//this->pAddress = arr.pAddress;

		//深拷贝
		this->pAddress = new T[m_Capacity];//注意时m_Capacity,而不是m_Size

		for (int i = 0; i < m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];

	}
重载=
代码语言:javascript
复制
 	//重载=
	MyArray& operator=(const MyArray& arr)//注意返回类型,如果是void,则在a = b = c时会出错,因为改变的不是本身的数据,而是拷贝对象的数据
	{
		//如果堆区有数据,要先释放再拷贝
		if (pAddress != NULL)/**/
		{
			delete[] pAddress;
			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[m_Capacity];

		for (int i = 0; i < m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];

		return *this;
	}
插入数据
代码语言:javascript
复制
 	//插入
	void MyInsert(T& e)
	{
		if (m_Size == m_Capacity)/**/
		{
			cout << "数组已存满" << endl;
			return;
		}

		this->pAddress[m_Size] = e;
		m_Size++;
	}
删除数据
代码语言:javascript
复制
	//删除
	void MyDelet()
	{
		if (m_Size == 0)
		{
			cout << "数组已为空" << endl;
			return;
		}

		m_Size--;
	}
通过下标访问
代码语言:javascript
复制
 	//通过下标访问
	//因为可能通过下标更改数据的值(a[0] = 100),所以要返回T&,  注意要是引用
	T& operator[](int idx)
	{
		return this->pAddress[idx];
	}
获取数组大小
代码语言:javascript
复制
	//获取数组大小
	int getSize()
	{
		return this->m_Size;
	}
获取数组容量
代码语言:javascript
复制
	//获取数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}
析构函数
代码语言:javascript
复制
	//析构函数
	~MyArray()
	{
		if (pAddress != NULL)/*防止重复释放*/
		{
			delete[] pAddress;
			pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}

.cpp框架

代码语言:javascript
复制
#include "shu_zu_lei_mu_ban.hpp"/*头文件名称*/
//自己创建的头文件用""访问更快,是直接去该文件路径下面查找文件
//用<>是直接库函数下查找文件,当要查找库中的文件时,用<>快

int main()
{
	return 0;
}
int类型数据测试
代码语言:javascript
复制
#include "shu_zu_lei_mu_ban.hpp"


//int数组
void PrintArr01(MyArray<int>& arr)
//注意参数中的类要写好类参数表<int>,因为类模板与函数模板不同,函数模板可以自动类型推导,而类模板不可
{
	int size = arr.getSize();
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

void test01()
{
	MyArray<int>arr(10);

	//放入数据
	int size = arr.getCapacity();
	for (int i = 0; i < size; i++)
	{
		arr.MyInsert(i);
	}

	//打印数组
	PrintArr01(arr);
}


int main()
{
	test01();
	return 0;
}
char类型测试
代码语言:javascript
复制
#include "shu_zu_lei_mu_ban.hpp"


//char数组

void PrintArr02(MyArray<char>& arr)
{
	int size = arr.getSize();
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

void test02()
{
	MyArray<char>arr(10);

	//放入数据
	arr.MyInsert('a');
	arr.MyInsert('c');
	arr.MyInsert('d');
	arr.MyInsert('e');


	//打印数组
	PrintArr02(arr);
}


int main()
{
	test02();
	return 0;
}

大家可以去自定义类测试,这里我就不陈述了

总代码

.hpp代码

代码语言:javascript
复制
#pragma once/**/

#include <iostream>

using namespace std;

template<typename T>
class MyArray
{
public:
	//有参构造
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		
		pAddress = new T[capacity];
	}

	//拷贝构造
	MyArray(const MyArray& arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

		//浅拷贝
		//this->pAddress = arr.pAddress;

		//深拷贝
		this->pAddress = new T[m_Capacity];//注意时m_Capacity,而不是m_Size

		for (int i = 0; i < m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];

	}
	 
	//重载=
	MyArray& operator=(const MyArray& arr)//注意返回类型,如果是void,则在a = b = c时会出错,因为改变的不是本身的数据,而是拷贝对象的数据
	{
		//如果堆区有数据,要先释放再拷贝
		if (pAddress != NULL)/**/
		{
			delete[] pAddress;
			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[m_Capacity];

		for (int i = 0; i < m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];

		return *this;
	}

	//插入
	void MyInsert(T e)
	{
		if (m_Size == m_Capacity)
		{
			cout << "数组已存满" << endl;
			return;
		}

		this->pAddress[m_Size] = e;
		m_Size++;
	}

	//删除
	void MyDelet()
	{
		if (m_Size == 0)
		{
			cout << "数组已为空" << endl;
			return;
		}

		m_Size--;
	}

	//通过下标访问
	//因为可能通过下标更改数据的值(a[0] = 100),所以要返回T&,  注意要是引用
	T& operator[](int idx)
	{
		return this->pAddress[idx];
	}

	//获取数组大小
	int getSize()
	{
		return this->m_Size;
	}

	//获取数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//析构函数
	~MyArray()
	{
		if (pAddress != NULL)/**/
		{
			delete[] pAddress;
			pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}
private:
	T* pAddress;
	int m_Size;
	int m_Capacity;
};

.cpp代码

代码语言:javascript
复制
#include "shu_zu_lei_mu_ban.hpp"


//int数组
void PrintArr01(MyArray<int>& arr)
{
	int size = arr.getSize();
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

void test01()
{
	MyArray<int>arr(10);

	//放入数据
	int size = arr.getCapacity();
	for (int i = 0; i < size; i++)
	{
		arr.MyInsert(i);
	}

	//打印数组
	PrintArr01(arr);
}

//char数组

void PrintArr02(MyArray<char>& arr)
{
	int size = arr.getSize();
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

void test02()
{
	MyArray<char>arr(10);

	//放入数据
	arr.MyInsert('a');
	arr.MyInsert('c');
	arr.MyInsert('d');
	arr.MyInsert('e');


	//打印数组
	PrintArr02(arr);
}


int main()
{
	//test01();
	test02();
	return 0;
}

题外话:

完结撒花~恭喜你又进步一点点啦~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍:
  • 分析:
  • 实现:
    • .hpp框架创建
      • .hpp函数内容
        • 有参构造
        • 拷贝构造:
        • 重载=
        • 插入数据
        • 删除数据
        • 通过下标访问
        • 获取数组大小
        • 获取数组容量
        • 析构函数
      • .cpp框架
        • int类型数据测试
        • char类型测试
    • 总代码
      • .hpp代码
        • .cpp代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档