Template<typename>自学

当用到同一个功能时,不在需要照葫芦画瓢了!
一个Template解决!!

#include
#include
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<typename>自学_第1张图片尝试到甜头了~
下一把试一下template

你可能感兴趣的:(Template<typename>自学)