自定义数组

我的初衷使用模板自定义一个数组,可以存储内置数据类型和自定义数据类型

首先,内置数据类型int测试是成功的

#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
	MyArray(int capacity)
	{
		cout << "有参构造调用" << endl;
		this->m_capacity = capacity;
		this->m_size = 0;
		this->pAddress = new T[this->m_capacity];
	}
	MyArray(const MyArray& arr)
	{
		cout << "拷贝构造调用" << endl;
		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];
		}
	}
	MyArray& operator=(const MyArray& arr)
	{
		cout << "operator=调用" << endl;
		//1.先判断原来堆区是否有数据,如果有,先释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
		this->m_capacity = 0;
		this->m_size = 0;
		//2.深拷贝
		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) 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 << "析构调用" << endl;
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:
	//指针指向堆区开辟的真实数组
	T* pAddress;
	//数组容量
	int m_capacity;
	//数组大小
	int m_size;
};
void PrintArray1(MyArray <int>& arr)
{
	for (int i = 0; i < arr.getsize(); i++)
	{
		cout << arr[i] << endl;
	}
}
void test01()
{
	MyArray <int>arr1(5);
	for (int i = 0; i < arr1.getCapacity(); i++)
	{
		arr1.push_back(i);
	}
	cout << "arr1打印输出为:" << endl;
	PrintArray1(arr1);
}

int main()
{
	test01();
	system("pause");
	return 0;
}

但当我自定义一个类person时却出了问题

 我也没想明白错在哪里,可能基础知识掌握不牢吧,

代码我也附上,大家如果能找到问题所在,说明也理解了相关知识了,

也欢迎大家留言告诉我哪里出错了‘。

#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
	MyArray(int capacity)
	{
		cout << "有参构造调用" << endl;
		this->m_capacity = capacity;
		this->m_size = 0;
		this->pAddress = new T[this->m_capacity];
	}
	MyArray(const MyArray& arr)
	{
		cout << "拷贝构造调用" << endl;
		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];
		}
	}
	MyArray& operator=(const MyArray& arr)
	{
		cout << "operator=调用" << endl;
		//1.先判断原来堆区是否有数据,如果有,先释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
		this->m_capacity = 0;
		this->m_size = 0;
		//2.深拷贝
		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) 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 << "析构调用" << endl;
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:
	//指针指向堆区开辟的真实数组
	T* pAddress;
	//数组容量
	int m_capacity;
	//数组大小
	int m_size;
};
void PrintArray1(MyArray <int>& arr)
{
	for (int i = 0; i < arr.getsize(); i++)
	{
		cout << arr[i] << endl;
	}
}
void test01()
{
	MyArray <int>arr1(5);
	for (int i = 0; i < arr1.getCapacity(); i++)
	{
		arr1.push_back(i);
	}
	cout << "arr1打印输出为:" << endl;
	PrintArray1(arr1);
}
class person
{
public:
	person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
void PrintArray2(MyArray <person>& arr)
{
	for (int i = 0; i < arr.getsize(); i++)
	{
		cout << "姓名:" << arr[i].m_name << "\t年龄:" << arr[i].m_age << endl;
	}
}
void test02()
{
	MyArray<person>arr2(2);
	for (int i = 0; i < arr2.getCapacity(); i++)
	{
		person p("万能人", i);
		arr2.push_back(p);
	}
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

先留一个链接,弄明白后发表在这篇文章里

https://blog.csdn.net/qq_64360901/article/details/123121204icon-default.png?t=M1L8https://blog.csdn.net/qq_64360901/article/details/123121204 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Koi279

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

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

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

打赏作者

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

抵扣说明:

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

余额充值