#include<iostream>
#include<string>
using namespace std;
typedef struct{
string name;
int age;
float score;
} STU;
//函数模板
template<class T> const T& Max(const T& a,const T& b);
//函数模板的显示具体化(针对STU类型的显示具体化)
template<> const STU& Max<STU>(const STU& a,const STU& b);
//重载
ostream & operator<<(ostream & out, const STU & stu);
int main(){
int a = 10;
int b = 20;
cout << Max(a, b) << endl;
STU stu1 = { "王明", 16,95.5 };
STU stu2 = { "许可", 17, 90 };
cout << Max(stu1, stu2) << endl;
return 0;
}
template<class T> const T& Max(const T& a,const T& b){
return a > b ? a : b;
}
template<> const STU& Max<STU>(const STU& a,const STU& b){
return a.score > b.score ? a : b;
}
ostream & operator<<(ostream &out,const STU &stu){
out << stu.name << ", " << stu.age << ", " << stu.score;
return out;
}
c++——函数模板的显示具体化
最新推荐文章于 2025-02-14 20:06:51 发布