ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ 类模板案例 (实现一个通用的数组类)

2021-02-21 19:29:43  阅读:149  来源: 互联网

标签:arr Capacity pAddress C++ int MyArray 数组 模板 Size


案例描述:实现一个通用的数组类,要求如下:

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

.hpp文件

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

template<class T>
class MyArray
{
public:
	//有参构造
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_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[arr->m_Capacity];
		//拷贝数据 
		for(int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];	
		}	
	} 
	//operator= 重载 防止浅拷贝问题 
	MyArray& operator=(const MyArray& arr)
	{
		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 Push_Back(const T& val)
	{
		//判断容量是否等于大小
		if(this->m_Capacity == this->m_Size)
		{
			cout << "已经满了 插入失败!" << endl;
			return;	
		} 
		this->pAddress[this->m_Size] = val;
		this->m_Size++;	//更新数组大小 
	} 
	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素
		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)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;	
		}	
	} 
private:
	T* pAddress;	//指针指向堆区开辟的真实数组
	
	int m_Capacity;	//数组容量
	
	int m_Size;		//数组大小 
};

带测试 总源码

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

template<class T>
class MyArray
{
public:
	//有参构造
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_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[arr->m_Capacity];
		//拷贝数据 
		for(int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];	
		}	
	} 
	//operator= 重载 防止浅拷贝问题 
	MyArray& operator=(const MyArray& arr)
	{
		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 Push_Back(const T& val)
	{
		//判断容量是否等于大小
		if(this->m_Capacity == this->m_Size)
		{
			cout << "已经满了 插入失败!" << endl;
			return;	
		} 
		this->pAddress[this->m_Size] = val;
		this->m_Size++;	//更新数组大小 
	} 
	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素
		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)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;	
		}	
	} 
private:
	T* pAddress;	//指针指向堆区开辟的真实数组
	
	int m_Capacity;	//数组容量
	
	int m_Size;		//数组大小 
};


void printIntArray(MyArray<int> &arr1)
{
	for(int i = 0; i < arr1.getSize(); i++)
	{
		cout << arr1[i] << endl;
	}
}
void test01()
{
	MyArray<int>arr1(5);
	for(int i = 0; i < 5; i++)
	{
		//尾插法添加数据
		arr1.Push_Back(i);
	}
	cout << "打印输出" << endl;
	printIntArray(arr1);
	//尾删 
	arr1.Pop_Back();
	cout << "容量为: " << arr1.getCapacity() << endl;
	cout << "大小为: " << arr1.getSize() << 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 << "姓名: " << arr[i].m_Name << " 年龄: "<< arr[i].m_Age << endl;
	}
}
void test02()
{
	MyArray<Person>arr(10);
	Person p1("小一",1);
	Person p2("小二",2);
	Person p3("小五",31);
	Person p4("小三",42);
	Person p5("小六",55);
	//插入
	arr.Push_Back(p1); 
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);
	//打印显示
	cout << "打印输出" << endl;
	printPersonArray(arr); 
	//尾删 
	arr.Pop_Back();
	cout << "容量为: " << arr.getCapacity() << endl;
	cout << "大小为: " << arr.getSize() << endl;	 
}
int main()
{
	test01();
	cout << "**自定义数据类型**" << endl;
	test02();

    system("pause");

	return 0;	
} 
 

标签:arr,Capacity,pAddress,C++,int,MyArray,数组,模板,Size
来源: https://blog.csdn.net/weixin_50901683/article/details/113922176

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有