函数模板的简单实例
1#include <iostream>
2
3using namespace std;
4
5template <typename T>//声明类型模版
6
7T Max(T a,T b)//声明函数模板
8{
9 a>b? a:b;
10 return a;
11}
12
13int main()
14{
15 int a,b;
16 float c,d;
17 cout<<"请输入a,b的值:";
18 cin>>a>>b;
19 cout<<"请输入c,d的值:";
20 cin>>c>>d;
21 cout<<Max(a,b)<<endl;
22 cout<<Max(c,d)<<endl;
23}
24
25
2
3using namespace std;
4
5template <typename T>//声明类型模版
6
7T Max(T a,T b)//声明函数模板
8{
9 a>b? a:b;
10 return a;
11}
12
13int main()
14{
15 int a,b;
16 float c,d;
17 cout<<"请输入a,b的值:";
18 cin>>a>>b;
19 cout<<"请输入c,d的值:";
20 cin>>c>>d;
21 cout<<Max(a,b)<<endl;
22 cout<<Max(c,d)<<endl;
23}
24
25