函数模板与类模板.docx
《函数模板与类模板.docx》由会员分享,可在线阅读,更多相关《函数模板与类模板.docx(19页珍藏版)》请在冰点文库上搜索。
函数模板与类模板
//-------------------------------------------------------
//求绝对值的函数模板,本例的绝对值函数可自动基于实参类型调整形参类型和返回值类型
//-------------------------------------------------------
#include
usingnamespacestd;
//-------------------------------------------------------
template
Tabs(Tx)
{returnx<0?
-x:
x;}
//-------------------------------------------------------
voidmain()
{
intn=-5;
doubled=-5.5;
cout<cout<}
//-------------------------------------------------------
//交换两个数的函数模板,本例Swap函数其形参可为任意相容类型
//-------------------------------------------------------
#include
usingnamespacestd;
//-------------------------------------------------------
template
voidSwap(T1&x,T2&y)
{
T1x1=x;
T2y1=y;
x=y1;
y=x1;
}
//-------------------------------------------------------
voidmain()
{
intx=8;
doubley=3.14;
cout<<"交换前:
x="<Swap(x,y);
cout<<"Swap后:
x="<}
//--------------------------------------------------------------------------
/*
类模板应用举例,本例示范了一个类模板Store,它可以对任意类型数据进行存取
体会本例,考虑实现一个通用的单项链表构造、遍历、添加节点、删除节点等算法
*/
//--------------------------------------------------------------------------
#include
usingnamespacestd;
//--------------------------------------------------------------------------
//结构体Student
structStudent
{
intid;//学号
floatgpa;//平均分
};
//--------------------------------------------------------------------------
template//类模板:
实现对任意类型数据进行存取
classStore
{
private:
Titem;//用于存放任意类型的数据
inthaveValue;//用于标记item是否已被存入内容
public:
Store(void);//默认形式(无形参)的构造函数
TGetElem(void);//提取数据函数
voidPutElem(Tx);//存入数据函数
};
//--------------------------------------------------------------------------
//默认形式构造函数的实现,将haveValue设置为0
template
Store:
:
Store(void):
haveValue(0)//直接将成员变量haseValue初始化为0
{}
//Store:
:
Store(void)
//{
//haveValue=0;
//}
//--------------------------------------------------------------------------
template//提取数据函数的实现
TStore:
:
GetElem(void)
{//如果试图提取未初始化的数据,则终止程序
if(haveValue==0)
{cout<<"item不存在!
程序终止"<exit
(1);
}
returnitem;//返回item中存放的数据
}
//--------------------------------------------------------------------------
template//存入数据函数的实现
voidStore:
:
PutElem(Tx)
{haveValue++;//将haveValue置为TRUE,表示item中已存入数值
item=x;//将x值存入item
}
//--------------------------------------------------------------------------
voidmain(void)
{
Studentg={1000,23};//此处使用的结构体初始化,相当于:
g.id=1000;g.gpa=23;
StoreS1,S2;//实例化类模板S1,S2它们均为int类型
StoreS3;//实例化类模板S3,它是自定义的结构体Student类型
StoreD;//实例化类模板D它们均为double类型
S1.PutElem(3);
S2.PutElem(-7);
cout<S3.PutElem(g);
//以下语句均是错误的,因为S3初始化时指明的模板是Student,故PutElem接收的参数也必须是Student
//S3.PutElem(3)
//S3.PutElem(s1)
cout<<"Thestudentidis"<cout<<"尝试获取D中的元素:
";
cout<//由于D未经初始化,在执行函数D.GetElement()时出错
}
//--------------------------------------------------------------------------
//动态数组类模板程序,本例可实现任意数值类型的数组,其长度动态改变,值得收藏借鉴
//--------------------------------------------------------------------------
#include
#include
#include
usingnamespacestd;
//--------------------------------------------------------------------------
template
classArray
{private:
T*alist;
intsize;
public:
Array(intsz=50);//构造函数
Array(constArray&A);//拷贝构造函数
~Array(void){if(alist)delete[]alist;}
Array&operator=(constArray&A);//重载"="运算符
T&operator[](inti);//重载下标操作符
intListSize(void)const{returnsize;}//返回数字大小
voidResize(intsz);//重置数组大小
};
//--------------------------------------------------------------------------
template
Array:
:
Array(intsz)
{
try
{
if(sz<=0)
{
chars[32];
sprintf(s,"无效的数组长度:
%d",sz);
throwexception(s);
}
alist=newT[sz];
size=sz;
//if(alist==NULL)...无效,new申请内存失败不会返回NULL
}
catch(...)
{
alist=NULL;
cout<<"未知错误:
可能内存不够"<}
}
//--------------------------------------------------------------------------
template
Array:
:
Array(constArray&A)
{
try
{
size=A.size;
alist=newT[size];
intn=szie;
T*pSrc=A.alist;
T*pDst=alist;
while(n--)*pDst++=*pSrc++;
}
catch(exception&e)
{
alist=NULL;
throwexception(e.what());
}
catch(...)
{
alist=NULL;
chars[32];
sprintf(s,"无效的数组长度:
%d",sz);
throwexception(s);
}
}
//--------------------------------------------------------------------------
template
Array&Array:
:
operator=(constArray&A)
{
try
{
if(A.alist==alist)return*this;//表明是自己赋给自己。
不做处理,否则执行下面代码将丢失数据
if(alist)delete[]alist;
size=A.size;
alist=newT[size];
intn=size;
T*pSrc=A.alist;
T*pDst=alist;
while(n--)*pDst++=*pSrc++;
return*this;
}
catch(exception&e)
{
alist=NULL;
throwexception(e.what());
}
catch(...)
{
alist=NULL;
throwexception("未知错误:
可能内存不够");
}
}
//--------------------------------------------------------------------------
template
T&Array:
:
operator[](inti)
{
if(i<0||i>=size)throwexception("数组下标越界");
returnalist[i];
}
//--------------------------------------------------------------------------
template
voidArray:
:
Resize(intsz)//重置数组大小
{
try
{
if(alist==NULL)throw("原始数组为空");
T*newlist=newT[sz];
memcpy(newlist,alist,sizeof(T)*size);
delete[]alist;
alist=newlist;
size=sz;
}
catch(exception&e)
{
throwexception(e.what());
}
catch(...)
{
throw("未知错误:
可能内存不够");
}
}
//--------------------------------------------------------------------------
voidmain(void)
{
try
{
ArrayA(10);
intn;
intprimecount=0,i,j;
cout<<"输入大于等于2的整数,用于求该整数以内的所有质数:
";
cin>>n;
A[primecount++]=2;//2是一个质数
for(i=3;i{
if(primecount==A.ListSize())//若得到的质数个数大于数组长度,则重置数组大小(增加10)
A.Resize(primecount+10);
if(i%2==0)continue;
j=3;
while(j<=i/2&&i%j!
=0)j+=2;
if(j>i/2)A[primecount++]=i;
}
for(i=0;i{cout<if((i+1)%10==0)cout<}
cout<//以下测试赋值运算符=
ArrayB(5);
B=A;
for(i=0;i{cout<if((i+1)%10==0)cout<}
cout<}
catch(exception&e)
{
cout<}
system("pause");
}
/动态数组类模板程序
#ifndefARRAY_CLASS
#defineARRAY_CLASS
usingnamespacestd;
#include
#include
#ifndefNULL
constintNULL=0;
#endif//NULL
enumErrorType//错误类型:
数组大小错误、内存分配错误、下标越界
{invalidArraySize,memoryAllocationError,
indexOutOfRange};
char*errorMsg[][]=
{"Invalidarraysize","Memoryallocationerror",
"Invalidindex:
"
};
template
classArray
{private:
T*alist;
intsize;
voidError(ErrorTypeerror,intbadIndex=0)const;
public:
Array(intsz=50);//ü
Array(constArray&A);//ü
~Array(void);
Array&operator=(constArray&rhs);//ü
T&operator[](inti);//ü
operatorT*(void)const;//ü
intListSize(void)const;
voidResize(intsz);
};
//构造函数
template
Array:
:
Array(intsz)
{
if(sz<=0)//sz为数组大小,若小于0,则输出错误信息
Error(invalidArraySize);
size=sz;//将元素个数赋值给变量size
alist=newT[size];//动态分配size个T类型的元素空间
if(alist==NULL)//如果分配内存不成功,输出错误信息
Error(memoryAllocationError);
}
template
Array:
:
Array(constArray&X)
{
intn=X.size;
size=n;
alist=newT[n][n];
if(alist==NULL)Error(memoryAllocationError);
T*srcptr=X.alist;//X.alist是对象X的数组首地址
T*destptr=alist;//alist是本对象中的数组首地址
while(n--)//逐个复制数组元素
*destptr++=*srcptr++;
}
template
Array&Array:
:
operator=(constArray&rhs)
{intn=rhs.size;
if(size!
=n)
{delete[]alist;
alist=newT[n];
if(alist==NULL)Error(memoryAllocationError);
size=n;
}
T*destptr=alist;
T*srcptr=rhs.alist;
while(n--)
*destptr++=*srcptr++;
return*this;
}
template
T&Array:
:
operator[](intn)
{
//检查下标是否越界
if(n<0||n>size-1)
Error(indexOutOfRange,n);
//返回下标为n的数组元素
returnalist[n];
}
#include
#include
#define_ROW_3
usingnamespacestd;
template
classTDArray{
public:
TDArray();
TDArray(introw);
~TDArray();
voidsetSize(introw);
intlength();
T*operator[](introw);
private:
T**data;
int*count;
intsize;
};
template
TDArray:
:
TDArray(){
data=newT*[_ROW_];
count=newint[_ROW_];
for(inti=0;i<_ROW_;i++)
data[i]=NULL;
count[i]=0;
size=0;
}
template
TDArray:
:
TDArray(introw){
intcol=0;
size=0;
data=newT*[row];
count=newint[row];
for(inti=0;icout<<"Inputnumberofelementsinrow"<
cin>>col;
if(col==0){
data[i]=NULL;
count[i]=0;
}
else{
data[i]=newT[col];
cout<<"Inputelementsinrow"<
for(intj=0;j
cin>>data[i][j];
count[i]=col;
size+=col;
}
}
}
template
TDArray:
:
~TDArray(){
delete[]data;
delete[]count;
}
template
voidTDArray:
:
setSize(introw){
delete[]data;
delete[]count;
intcol=0;
size=0;
data=newT*[row];
count=newint[row];
for(inti=0;icout<<"Inputnumberofelementsinrow"<
cin>>
|
|