前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++类模板案例-构建自己的通用数组

C++类模板案例-构建自己的通用数组

原创
作者头像
BrianLee
发布2023-04-06 11:01:32
5730
发布2023-04-06 11:01:32
举报
文章被收录于专栏:用户1031264的专栏

一、运用到类模板、拷贝构造函数、深拷贝、运算符重载、尾插法、尾删法

  • MyArray.hpp
代码语言:c++
复制
#pragma once
//通用的数组类
#include <iostream>
using namespace std;
template<class T>
class MyArray 
{
private:
	T* pAddress;				//指针指向堆区开辟的真实的数组

	int m_Capacity;				//数组容量

	int m_Size;					//元素个数

public:

	//有参构造 参数 容量
	MyArray(int capacity):m_Capacity(capacity)
	{
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
		// << "MyArray的有参构造调用" << endl;

	}

	//拷贝构造
	MyArray(const MyArray& arr) 
	{
		//cout << "MyArray的拷贝构造调用" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//深拷贝
		this->pAddress = new T[arr.m_Capacity];


		//将arr中数据copy过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}


	//operator= 防止浅copy问题
	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;
		}


		//深copy
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < arr.m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}


	//尾插法
	void Push_Back(const T& val) 
	{
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		this->pAddress[this->m_Size] = val;					//在数组末尾插入数据
		this->m_Size++;										//更新数组大小
	
	}


	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素,即为尾删,逻辑删除
		if (this->m_Size ==0)
		{
			return;
		}
		this->m_Size--;
	
	
	}


	//通过下标的方式访问数组中的元素
	//返回T& 可以作为左值使用  arr[0]=100

	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;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}

};

  • 类模板案例-数组类封装.cpp
代码语言:javascript
复制
#include <iostream>
using namespace std;
#include "MyArray.hpp"
#include <string>

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

}

void test01() 
{
	MyArray<int> arr1(5);
	//MyArray<int> arr2(arr1);

	//MyArray<int> arr3(100);
	//arr3 = arr1;

	for (int i = 0; i < 5; i++)
	{
		arr1.Push_Back(i);						//尾插法向数组中插入数据
	}
	cout << "arr1的打印输出" << endl;

	PrintIntArray(arr1);
	cout << "arr1的容量为:" << arr1.getCapacity() << endl;
	cout << "arr1的大小为:" << arr1.getSize() << endl;

	MyArray<int> arr2(arr1);
	cout << "arr2的打印输出" << endl;
	PrintIntArray(arr2);

	arr2.Pop_Back();
	cout << "arr2尾删后:" << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;

}

//测试自定义的数据类型
class Person
{
public:
	string m_Name;
	int m_Age;
	Person() {};
	Person(string name, int age) :m_Name(name), m_Age(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("孙悟空", 999);
	Person p2("韩信", 20);
	Person p3("妲己", 30);
	Person p4("赵云", 25);
	Person p5("安其拉", 27);

	//将数据插入到数组中
	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();
}

二、运算结果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、运用到类模板、拷贝构造函数、深拷贝、运算符重载、尾插法、尾删法
  • 二、运算结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档