




如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
第12章模板12.1概述12.2函数模板#include<iostream.h> template<classT> Tmin(Ta[],intn) { inti; Tminv=a[0]; for(i=1;i<n;i++) if(minv>a[i]) minv=a[i]; returnminv; }重载函数模板12.3类模板例12.4堆栈类模板的编写与使用template<classT> Stack<T>::Stack(intMaxStackSize) { //Stack类构造函数 MaxTop=MaxStackSize-1; stack=newT[MaxStackSize]; top=-1; } template<classT> TStack<T>::Top()const { //返回栈顶元素 if(IsEmpty())cout<<"Outofbounds!"<<endl; elsereturnstack[top]; } template<classT> Stack<T>&Stack<T>::Push(constT&x) { //添加元素x if(IsFull())cout<<"Stackisfull!"<<endl; else stack[++top]=x; return*this; }template<classT> Stack<T>&Stack<T>::Pop(T&x) { //删除栈顶元素,并将其送入x if(IsEmpty())cout<<"Outofbounds!"<<endl; else x=stack[top--]; return*this; } #endif //stack.cpp //测试使用Stack堆栈模板类 #include<iostream.h> #include"stack.h" voidmain() {inti; Stack<int>intStack; cout<<"处理整型堆栈"<<endl; for(i=0;i<4;i++) { intStack.Push(i); cout<<"压入整型堆栈:"<<i<<endl; } for(i=0;i<4;i++) { intj; intStack.Pop(j); cout<<"从整型堆栈中弹出:"<<j<<endl; } Stack<float>floatStack; cout<<"处理浮点型堆栈"<<endl; for(i=0;i<4;i++) {floatf=i*1.1f; floatStack.Push(f); cout<<"压入浮点型堆栈:"<<f<<endl; } for(i=0;i<4;i++) { floatj; floatStack.Pop(j); cout<<"从浮点型堆栈中弹出:"<<j<<endl; } }珍烙惜酶野呈驳杨稀吕惦霍码娩扣绅囚弱油艇疥付完舒迅饲协苇烈除氰腋第12章模板第12章模板12.4用类模板构造简单线性表//llist.h //简单线性表类模板定义 #ifndefLLIST_H #defineLLIST_H #include<stdlib.h> template<classT> classLinearList{ public: LinearList(intMaxListSize=10); //构造函数 ~LinearList(){delete[]element;} //析构函数 boolIsEmpty()const{returnlength==0;}//测线性表是否为空 intLength()const{returnlength;} //返回线性表的长 boolFind(intk,T&x)const; //返回第K个元素的值至x中 intSearch(constT&x)const; //返回x所在位置 LinearList<T>&Delete(intk,T&x);//删除第k个元素并将它值返回至x中 LinearList<T>&Insert(intk,constT&x);//在第k个元素之后插入x voidOutput(ostream&out)const; private: intlength; intMaxSize; T*element;//一维动态数组 }; template<classT> LinearList<T>::LinearList(intMaxListSize) {//线性表构造函数 MaxSize=MaxListSize; element=newT[MaxSize]; length=0; }template<classT> boolLinearList<T>::Find(intk,T&x)const {//把第k个元素取

xx****88
实名认证
内容提供者


最近下载
一种胃肠道超声检查助显剂及其制备方法.pdf
201651206021+莫武林+浅析在互联网时代下酒店的营销策略——以湛江民大喜来登酒店为例.doc
201651206021+莫武林+浅析在互联网时代下酒店的营销策略——以湛江民大喜来登酒店为例.doc
用于空间热电转换的耐高温涡轮发电机转子及其装配方法.pdf
用于空间热电转换的耐高温涡轮发电机转子及其装配方法.pdf
用于空间热电转换的耐高温涡轮发电机转子及其装配方法.pdf
用于空间热电转换的耐高温涡轮发电机转子及其装配方法.pdf
用于空间热电转换的耐高温涡轮发电机转子及其装配方法.pdf
用于空间热电转换的耐高温涡轮发电机转子及其装配方法.pdf
论《离骚》诠释史中的“香草”意蕴.docx