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

1.可以对内置数据类型以及自定义数据类型的数据进行存

2.将数组中的数据存储到堆区

3.构造函数中可以传入数组的容量

4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题提供尾插法和尾删法对数组中的数据进行增加和删除

5.可以通过下标的方式访问数组中的元素

6.可以获取数组中当前元素个数和数组的容量

注意事项:因为模板的调用机制,所以这里需将头文件以及源文件的实现都写入后缀名为“.hpp”的文件中。

以下是源码:

“myArray.hpp”

#pragma once
#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];

		//将arr中的数组都拷贝过来
		for (int i = 0; i < this->m_Size ;i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}

	}


	//operator= 防止浅拷贝问题
	MyArray& operator=(const MyArray& arr)
	{
		cout << "operator=函数调用" << endl;
		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)
		{
			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 Getcap()
	{
		return this->m_Capacity;
	}

	//返回数组大小
	int Getsize()
	{
		return this->m_Size;
	}


	~MyArray()
	{
		//cout << "析构函数调用" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL; 
		}
	}



private:

	T* pAddress;//指针指向堆区开辟的真实数据

	int m_Capacity;//数组容量

	int m_Size;//数组大小

};

"mian.cpp"

#include<iostream>
using namespace std;
#include"myArray.hpp"
#include<string>

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


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

	for (int i = 0; i < 5; i++)
	{
		arr.Push_back(i);
	}
	cout << "arr的打印输出为:" << endl;

	print(arr);

	cout << "arr的容量为:" << arr.Getcap() << endl;
	cout << "arr的大小为:" << arr.Getsize() << endl;



	MyArray<int>arr2(arr);
	cout << "arr2的打印输出为:" << endl;
	print(arr2);

	arr2.Pop_back();
	cout << "arr2尾删后" << endl;
	cout << "arr2的容量为:" << arr2.Getcap() << endl;
	cout << "arr2的大小为:" << arr2.Getsize() << endl;
	//MyArray<int>arr3(100);

	//arr3 = arr;

}

class person
{
public:
	person()
	{

	}

	person(string name,int age)
	{
		this->m_name = name;
		this->m_age = age;
	}

	string m_name;
	int m_age;
};


void inprintf(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("孙悟空", 99);
	person p2("八戒", 88);

	arr.Push_back(p1);
	arr.Push_back(p2);
	inprintf(arr);
	cout << "arr的容量为:" << arr.Getcap() << endl;
	cout << "arr的大小为:" << arr.Getsize() << endl;
}


int main()
{
	//test01();
	test02();

	system("pause");
	return 0;
}

test01与test02用作测试,测试效果如下:

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐