其实举个简单的例子反而更利于新手理解本质。
函数模板:
#include
usingnamespace std;
template<typename T1>
voidSwap(T1& a, T1& b)
{
T1 t = a;
a = b;
b = t;
}
int main()
{
int a =2;
int b =3;
cout <<"a = "<< a <<"; b = "<< b <<endl;
Swap(a,b);
cout <<"a = "<< a <<"; b = "<< b <<endl;
float c =0.02;
float d =0.03;
cout <<"c = "<< c <<"; d = "<< d <<endl;
Swap(c,d);
cout <<"c = "<< c <<"; d = "<< d <<endl;
return0;
}