9.13(周三)[1]

函数模板中使用多种类型

#include<iostream>

using namespace std;
template <class T1,class T2>


void Swap( T1&a,T2&b)
{
T1 t;
t=a;
a=T1(b);
b=T2(t);
}
void main()
{
int x=5;
char y='c';
cout<<x<<","<<y<<endl;
Swap(x,y);
cout<<char(x)<<","<<int(y)<<endl;
system("pause");

}

函数模板 实例

#include<iostream>
using namespace std;
template <class T>
T max(T x,T y);


void main()
{
float a=5.2,b=8.5;
char c='d',d='D';
cout<<Max(a,b)<<endl;
cout<<Max(c,d)<<endl;
system("pause");
}
template<class T>


T Max( T x,T y)
{
if (x>=y) return x;
return y;
}


函数模板重载

#include<iostream>
using namespace std;


float sum(float One,float Two)
{
return One+Two;
}


template <class T>
T sum( T One, T two, T three)
{
return One+two+three;


}
void main()


{
int a=1,b=2,c=3;
cout<<sum(a,b)<<endl;
cout<<sum(a,b,c)<<endl;
system("pause");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值