作者在 2010-05-07 15:20:26 发布以下内容
#include "iostream.h"
#include "string.h"
//以下通过重载函数模板,定义了2个模板
//定义模板
template <class TYPE> //定义第一个模板
TYPE max(TYPE a,TYPE b)
{
return (a>b)?a:b;
}
template <class TYPE> //定义第二个模板
TYPE max(TYPE a,TYPE b,TYPE c)
{
TYPE t;
t=(a>b)?a:b;
return (t>c)?t:c;
}
//重写一个非函数模板来专门处理这种特殊情况
char *max(char *a,char *b)
{
return (strcmp(a,b)>0)?a:b;
}
#include "string.h"
//以下通过重载函数模板,定义了2个模板
//定义模板
template <class TYPE> //定义第一个模板
TYPE max(TYPE a,TYPE b)
{
return (a>b)?a:b;
}
template <class TYPE> //定义第二个模板
TYPE max(TYPE a,TYPE b,TYPE c)
{
TYPE t;
t=(a>b)?a:b;
return (t>c)?t:c;
}
//重写一个非函数模板来专门处理这种特殊情况
char *max(char *a,char *b)
{
return (strcmp(a,b)>0)?a:b;
}
void main(void)
{
int x=10,y=20,max2;
double a=10.3,b=21.7,c=14.5,max3;
max2=max(x,y);
max3=max(a,b,c);
char max1=max(*"WYZ",*"ABC"); //这里不加*的时候编译过不去
cout<<"The maximum of "<<x<<" and "<<y<<" is: "
<<max2<<endl;
cout<<"The maximum of "<<a<<", "<<b<<" and "<<c<<"is "
<<max3<<endl;
cout<<"The maximum of\"WYZ\"and\"ABC\" is: "<<max1<<endl; //\"是转义符,输出双引号用
}
{
int x=10,y=20,max2;
double a=10.3,b=21.7,c=14.5,max3;
max2=max(x,y);
max3=max(a,b,c);
char max1=max(*"WYZ",*"ABC"); //这里不加*的时候编译过不去
cout<<"The maximum of "<<x<<" and "<<y<<" is: "
<<max2<<endl;
cout<<"The maximum of "<<a<<", "<<b<<" and "<<c<<"is "
<<max3<<endl;
cout<<"The maximum of\"WYZ\"and\"ABC\" is: "<<max1<<endl; //\"是转义符,输出双引号用
}