c++实现一个简易vector加强版

主要在原来的基础上引入了模板类

template<typename T>或者template<class T>均可

class Name_Of_Class
{
// ......
};

作用域解析符之前不再只是Name_Of_Class,而变成了Name_Of_Class<T>,在本类变量作函数参数时也有同样变化

比如构造函数

Name_Of_Class<T>::Name_Of_Class()
{
//......
}

复制构造函数

Name_Of_Class<T>::Name_Of_Class(const  Name_Of_Class<T>&  the_object)
{
//......
}

在每个成员函数的实现之前都要加上

template<typename T>


代码如下:

using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::endl;

template<typename T>
class VectorT
{
public:
	bool operator =(const VectorT<T>&); 
	friend bool operator ==(const VectorT<T>&,const VectorT<T>&);

	int capasity();
	int size();
	bool push_back(T);
	bool pop_back();

	bool reserve(int);
	bool resize(int);
	bool empty();
	T value_at(int);
	void change_value_at(int,T);
	void traval();

	VectorT();
	VectorT(int);
	VectorT(VectorT<T>&);
	~VectorT();



private:
	bool full();
	T *p;
	int count;
	int max_count;
};
template<typename T>
void VectorT<T>::traval()
{
	for(int i=0;i<count;i++)
	{
		cout<<p[i]<<" ";
	}
	cout<<endl;
}
template<typename T>
bool VectorT<T>::operator =(const VectorT<T>& temp)
{
	if(max_count>=temp.count)
	{
		count=temp.count;
		max_count=temp.max_count;
		p=new T[max_count];
		for(int i=0;i<temp.count;i++)
			p[i]=temp.p[i];
	}
	else
	{
		delete [] p;
		p=new T[temp.count];
		for(int i=0;i<temp.count;i++)
			p[i]=temp.p[i];
	}
	return true;
}
template<typename T>
bool operator ==(const VectorT<T>& data1,const VectorT<T>& data2)
{
	bool flag=true;
	if(data1.count!=data2.count && flag==true)
		flag=false;
	if(flag==true)
	{
		for(int i=0;i<data1.count;i++)
		{
			if(data1.p[i]!=data2.p[i])
			{
				flag=false;
				break;
			}
		}
	}
	return flag;
}
/*******************************************/
template<typename T>
VectorT<T>::VectorT()
{
	p=new T[50];
	count=0;
	max_count=50;
}
template<typename T>
VectorT<T>::VectorT(int length)
{
	p=new T[length];
	count=0;
	max_count=length;
}
template<typename T>
VectorT<T>::VectorT(VectorT<T>& temp)
{
	count=temp.count;
	max_count=temp.max_count;
	p=new T[max_count];
	for(int i=0;i<temp.count;i++)
		p[i]=temp.p[i];
}
/*******************************************/
template<typename T>
VectorT<T>::~VectorT()
{
	delete [] p;
}
/*******************************************/
template<typename T>
bool VectorT<T>::full()
{
	if(count==max_count)
		return true;
	else
		return false;
}
template<typename T>
bool VectorT<T>::empty()
{
	if(count==0)
		return true;
	else
		return false;
}

/*******************************************/
template<typename T>
int VectorT<T>::capasity()
{
	return max_count;
}
template<typename T>
int VectorT<T>::size()
{
	return count;
}
/*******************************************/
template<typename T>
bool VectorT<T>::reserve(int length)
{
	if(length<=max_count)
		return false;

	T *temp;
	temp=new T[max_count];
	for(int i=0;i<count;i++)
	{
		temp[i]=p[i];
	}
	delete [] p;

	max_count=length;
	p=new T[max_count];
	for(i=0;i<count;i++)
	{
		p[i]=temp[i];
	}
	delete [] temp;

	return true;
}
template<typename T>
bool VectorT<T>::resize(int length)
{
	if(length<=max_count)
	{
		count=length;
	}
	else if(length>max_count)
	{
		count=length;
		reserve(length);
	}
	return true;
}
template<typename T>
bool VectorT<T>::push_back(T data)
{

	if(full())
	{
		reserve(max_count*2);
		p[count++]=data;
	}
	else
	{
		p[count++]=data;
	}

	return true;
}
template<typename T>
bool VectorT<T>::pop_back()
{
	if(empty())
	{
		cout<<"VectorT Empty"<<endl;
		exit(1);
	}
	else
	{
		count--;
	}
	return true;
}


/*******************************************/
template<typename T>
T VectorT<T>::value_at(int i)
{
	if(i>=count)
	{
		cout<<"Error location"<<endl;
		return -1;
	}
	return p[i];
}
template<typename T>
void VectorT<T>::change_value_at(int i,T data)
{
	if(i>=count)
	{
		cout<<"Error location"<<endl;
	}
	p[i]=data;
}
/*******************************************/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值