您的位置:首页 > 编程语言 > C语言/C++

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

2011-08-08 13:54 603 查看


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

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;
}
/*******************************************/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vector c++ delete class object