📄 templates.cpp
字号:
#include <iostream.h>
#include <string.h>
//【例14.1】定义swap()函数,对两个数据进行交换,这两个数据都是整型或实型。
void swap(int &a ,int &b)
{
int temp=a;
a=b;
b=temp;
}
void swap(double &a ,double &b)
{
double temp=a;
a=b;
b=temp;
}
void main14_1()
{
int a=2,b=3;
double x=2.3,y=3.4;
swap(a,b);
swap(x,y);
}
//【例14.2】求两个相同类型参数的最大值函数max(a,b)。
/*在没有函数模板的前提下,可以采用宏定义语句实现,表示如下:
#define max(a,b) ((a>b)?a:b)
然而,由于#define语句只能进行字符串的替换,不能检查类型的合法,完全可能发生在两个不同类型参数进行
求最大值。若发生一个int型参数和一个Point类型的参数进行比较大小,会导致程序运行时发生故障。
定义一个函数模板就可以完全解决上述问题。*/
template <class T> T max(T& a,T& b) //定义max的函数模板
{
return a>b?a:b;
}
void main14_2()
{
cout<<"Max(2,6) is "<<max(3,5)<<endl;
cout<<"Max('A','D') is "<<max('A','D')<<endl;
}
//【例14.3】重载模板函数max的综合应用实例。
class Point
{ //类Point的定义
int x,y;
public:
Point(void) {x=0;y=0;}
Point(int i,int j) {x=i;y=j;}
friend int operator>(Point& a,Point& b); //对Point类进行重载运算符'>'
friend ostream& operator<<(ostream& os,Point& rp);
};
int operator>(Point& a,Point& b)
{
int distance;
distance=a.x*a.x+a.y*a.y-b.x*b.x-b.y*b.y; //判断两坐标离原点距离的长短
return distance>0?1:0;
}
ostream& operator<<(ostream& os,Point& p)
{
os<<"P("<<p.x<<","<<p.y<<")"; //输出的Point对象的坐标(x,y)值
return os;
}
template <class T> T max3(T &a,T&b) //定义函数模板max
{
return a>b?a:b;
}
char* max3(char *a,char *b) //重载模板函数max用来比较两个字符串
{
return (strcmp(a,b)>0?a:b);
}
void main14_3()
{
int a=3,b=5;
char c='E',d='G';
Point p1(3,5),p2(2,8);
cout<<"(1)max3(a,b) is :"<<max3(a,b)<<endl; //调用模板函数int max(int,int)
cout<<"(2)max3(c,d) is: "<<max3(c,d)<<endl; //调用模板函数char max(char,char)
cout<<"(3)max3(p1,p2) is: "<<max3(p1,p2)<<endl;
//调用模板函数Point max(Point,Point)
cout<<"(4)max(s1,s2) is "<<max3("Hello","Welcome")<<endl;
//调用重载非模板函数char * max(char*,char*)
}
//【例14.4】类模板的实例化
/*下面以一个通用的堆栈类为例,说明模板类的定义。堆栈是先进后出(FILO)的一个数据结构,
可以存放各种不同的数据类型数据,如int、char、float、double或类对象等。不同的数据类型代表
不同的类,但各种不同类之间的操作及成员变量是相同的,只是堆栈中所处理的数据类型不同而已。*/
template <class T>
class Stack
{
int size; //堆栈大小size
int top; //栈顶指针top
T* stackPtr; //指向堆栈存贮空间的指针stackPtr
public:
Stack(int=10); //Stack的构造函数,默认堆栈大小为10
~Stack() {delete[] stackPtr;} //Stack的析构函数
bool push(const T&); //入栈操作
bool pop(T&); //出栈操作
bool isEmpty() const {return top==-1;} //判断堆栈是否为空
bool isFull() const {return top==size-1;} //判断堆栈是否为满
};
template <class T>
Stack<T>::Stack(int s)
{
size=s>0?s:10; //设置堆栈空间大小为默认值10或为参数传递值s
top=-1; //设置初始栈顶值top=-1
stackPtr=new T[size]; //申请数据类型为T类型,空间大小为size的堆栈存贮空间
}
template <class T>
bool Stack<T>::push(const T& pushValue)
{
if (!isFull()) //判断椎栈是否已满
{
stackPtr[++top]=pushValue; //将元素pushValue入栈
return true;
}
return false;
}
template <class T>
bool Stack<T>::pop(T& popValue)
{
if (!isEmpty()) //判断椎栈是否为空
{
popValue=stackPtr[top--]; //将栈顶元素popValue出栈
return true;
}
return false;
}
void main14_4()
{
Stack<double> doubleStack(5);
double f=0.1;
cout<<"Pushing elements onto doubleStack\n";
while(doubleStack.push(f)){
cout<<f<<" ";
f++;
}
cout<<"\nStack is full. Cannot push "<<f
<<"\n\nPoping elements from doubleStack\n";
while(doubleStack.pop(f))
cout<<f<<" ";
cout<<"\n Stack is empty. Cannot pop\n";
Stack<int> intStack;
int i=1;
cout<<"\nPushing elements onto intStack\n";
while(intStack.push(i)){
cout<<i<<" ";
++i;
}
cout<<"\nStack is full.Cannot push "<<i<<
"\n\nPoping elements from doubleStack\n";
while(intStack.pop(i))
cout<<i<<" ";
cout<<"\n Stack is empty. Cannot pop\n";
}
//【例14.5】 利用模板对象作为函数模板的形参完成例14.4同样的功能。
#include <iostream.h>
template <class T>
void testStack(Stack<T>& theStack,T value,T increment,const char* stackName)
{
cout<<"\nPushing elements onto "<<stackName<<'\n';
while(theStack.push(value))
{
cout<<value<<" ";
value+=increment;
}
cout<<"\nStack is full. Cannot push "<<value
<<"\n\nPoping elements from "<<stackName<<'\n';
while(theStack.pop(value))
cout<<value<<" ";
cout<<"\nStack is empty. Cannot pop\n";
}
void main14_5()
{
Stack<double> doubleStack(5);
Stack<int> intStack;
testStack(doubleStack,0.1,1.0,"doubleStack");
testStack(intStack,1,1,"intStack");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -