Merge pull request #264 from Not-envious-of-immortals/master

修改模板特化代码bug
This commit is contained in:
Francis 2023-02-22 16:12:03 +08:00 committed by GitHub
commit 20eb89e15d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,14 +8,19 @@ T Max(T a,T b) {
} }
//特化 //特化
//template <> 返回类型 函数名<特化的数据类型>(参数表) {} //template <> 返回类型 函数名<特化的数据类型>(参数表) {}
template<>const char * Max<const char *>(const char *a,const char *b) {
return (strcmp(a,b)>=0)?a:b;
}
template<>char * Max<char *>(char *a,char *b) { template<>char * Max<char *>(char *a,char *b) {
return (strcmp(a,b)>=0)?a:b; return (strcmp(a,b)>=0)?a:b;
} }
int main(){ int main(){
float c=5.1,d=3.2; float c=5.1,d=3.2;
cout<<"2,3的最大值是"<<Max(3,2)<<endl; char s1[]="xyce", s2[]="xbv";
cout<<"c,d的最大值是"<<Max(c,d)<<endl; cout<<"2,3的最大值是"<<Max(3,2)<<endl;
cout<<Max("xbv","xyce")<<endl; cout<<"c,d的最大值是"<<Max(c,d)<<endl;
cout<<Max("xbv","xyce")<<endl;
cout<<Max(s1,s2)<<endl;
system("pause"); system("pause");
} }
/* /*