1. 程式人生 > >編寫程式利用模板交換不同型別的兩個數

編寫程式利用模板交換不同型別的兩個數

#include<iostream>
using namespace  std;
template<typename Type>
Tswap(Type &x,Type &y)
{
	Type temp=x;x=y;y=temp;

}

int main()
{
		int x=2,int y=5;
		cout<<"交換前:x="<<x<<","<<"y="<<y<<endl;
		Tswap(x,y);
		cout<<"交換後:x="<<x<<","<<"y="<<y<<endl;
		float a=2.3,b=34.5;
		cout<<"交換前:a="<<a<<","<<"b="<<b<<endl;
		Tswap(a,b);
		cout<<"交換後:a="<<a<<","<<"b="<<b<<endl;
		double c=2.43,d=3.44;
		cout<<"交換前:c="<<c<<","<<"d="<<d<<endl;
		Tswap(c,d);
		cout<<"交換後:c="<<c<<","<<"d="<<d<<endl;
		return 0;
}