C++类模板数组

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

/************************************************************************
数组模板类
1.内置数据类型和自定义数据类型进行存储
2.将数组中的数据存储到堆区
3.构造函数中传入数组的容量
4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题
5.提供尾插法和尾删法对数组中的数据进行增加和删除操作
6.可以通过下标的方式访问数组中的元素
7.可以获取数组中当前元素个数和数组的容量
/************************************************************************/



template <class T>
class ArrayTmp{

public:
	ArrayTmp(int capacity )
	{
		cout<<"构造函数"<<endl;
		this->m_Capacity = capacity;
		this->m_size = 0;
		this->m_pArray = new T[this->m_Capacity];
	}
	
	ArrayTmp(ArrayTmp &array)
	{
		cout<<"拷贝构造函数"<<endl;

		this->m_Capacity = array.m_Capacity;
		this->m_size = array.m_size;
		this->m_pArray = new T[this->m_Capacity];
		
		for(int i = 0; i<this->m_size; i++)
		{
			this->m_pArray[i] = array.m_pArray[i];
		}
	}
	
	//重载赋值运算符
	ArrayTmp& operator=(ArrayTmp &array)
	{

		cout<<"重载operator=成员函数"<<endl;

		if (this->m_pArray != NULL)
		{
			delete this->m_pArray;
			this->m_Capacity = 0;
			this->m_size = 0;
		}
		
		this->m_Capacity = array.m_Capacity;
		this->m_size = array.m_size;
		this->m_pArray = new T[this->m_Capacity];
		int i= 0;
		for(i = 0; i<this->m_size; i++)
		{
			this->m_pArray[i] = array.m_pArray[i];
		}
		return *this;
	}

	//重载[]运算符
	T operator[](int index)
	{
	
		return this->m_pArray[index];
	}

	int getCount()
	{
		return this->m_size;
	}

	int getCapacity()
	{
		return this->m_Capacity;
	}
	
	//尾插入
	void append(const T &item)
	{
		if (m_size == m_Capacity)
		{
			cout<<__LINE__<<endl;
			T* pArray_tmp = new T[m_Capacity+5];
			for (int i=0; i<m_size; i++)
			{
				pArray_tmp[i] = this->m_pArray[i];
			}
			delete [] this->m_pArray;
			this->m_pArray = pArray_tmp;
			this->m_Capacity += 5;
		}

		this->m_pArray[this->m_size] = item;
		this->m_size++;
	}

	//尾删除
	void pop()
	{		
		this->m_size--;
	}

	~ArrayTmp()
	{
		if (this->m_pArray != NULL)
		{
			delete []this->m_pArray; //m_pArray是个数组,需要delete []
			this->m_pArray = NULL;
		}
	}

private:
	T *m_pArray;
	int m_Capacity;
	int m_size;
};


void showPrint(ArrayTmp<int> &array)
{
	for (int i = 0; i<array.getCount(); i++)
	{
		cout<<array[i]<<" ";
	}
	cout<<endl;
}

//内置数据类型
void test1()
{
	ArrayTmp<int> array1(3);

	for (int i=0; i<20; i++)
	{
		array1.append(i);
	}

	showPrint(array1);
	cout<<"mem_count:"<<array1.getCount()<<endl;
	cout<<"capacity:"<<array1.getCapacity()<<endl;

	ArrayTmp<int> array2(array1);
	array2.pop();
	showPrint(array2);
	cout<<"mem_count:"<<array2.getCount()<<endl;
	cout<<"capacity:"<<array2.getCapacity()<<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 showPersonPrint(ArrayTmp<Person> &array)
{
	for (int i = 0; i<array.getCount(); i++)
	{
		cout<<"name:"<<array[i].m_Name<<" "<<"age:"<<array[i].m_Age<<endl;
	}
}

void test2()
{
	Person per1("张三", 22);
	Person per2("李四", 23);
	Person per3("王五", 24);
	Person per4("赵六", 25);
	Person per5("马七", 26);

	ArrayTmp<Person> array2(1);
	array2.append(per1);
	array2.append(per2);
	array2.append(per3);
	array2.append(per4);
	array2.append(per5);
	array2.pop();
	showPersonPrint(array2);
	cout<<"mem_count:"<<array2.getCount()<<endl;
	cout<<"capacity:"<<array2.getCapacity()<<endl;
}
int main()
{
	//test1();
	test2();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值