函数模板与类模板.docx
- 文档编号:15268523
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:19
- 大小:18.47KB
函数模板与类模板.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; Store Store Store 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 ~Array(void){if(alist)delete[]alist;} Array 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 { 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 : operator=(constArray { 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 { Array 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< //以下测试赋值运算符= Array 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 ~Array(void); Array 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 { 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 : operator=(constArray {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;i cout<<"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;i cout<<"Inputnumberofelementsinrow"< cin>>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 函数 模板