当用到同一个功能时,不在需要照葫芦画瓢了!
一个Template解决!!
#include<iostream>
#include<cstring>
using namespace std;
template<typename T>
void Swap(T &a,T &b)
{
T tem;
tem = a;
a = b;
b = tem;
}
template<typename t>
t hi(t a, t b)
{
t c;
c = a+b;
return c;
}
int main()
{
int a = 2;
int b = 3;
Swap(a,b);
cout << "a = " << a << ",b = " << b<< endl;
cout << "a + b =" << hi(a,b) << endl;
double c;
double d;
c = 1.1;
d = 2.656;
Swap(c,d);
cout << "c = " << c << ",d = " << d<< endl;
cout << "c + d = " << hi(c,d) << endl;
return 0;
}
结果:
尝试到甜头了~
下一把试一下template