Bootstrap

试着编写用模板函数MySwap实现对不同类型的数据进行交换,并在主函数中机型测试。

#include<iostream>
#include<string>
using namespace std;
template<typename T>
void Myswap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

int main()
{
	int a = 10;
	int b = 20;
	char c = 'c';
	char d = 'd';
	float e = 1.1;
	float f = 2.2;
	cout << "a=" << a << "  " << "b=" << b << endl;
	cout << "c=" << c << "  " << "d=" << d << endl;
	cout << "e=" << e << "  " << "f=" << f << endl;
	Myswap(a, b);
	cout << "Myswap(a, b)" << endl;
	cout << "a=" << a << "  " << "b=" << b << endl;
	Myswap(c, d);
	cout << "Myswap(c, d)" << endl;
	cout << "c=" << c << "  " << "d=" << d << endl;
	Myswap(e, f);
	cout << "Myswap(e, f)" << endl;
	cout << "e=" << e << "  " << "f=" << f << endl;
}

;