#include <iostream>
template<typename Any>
void Swap(Any&, Any&);
int main()
{
using namespace std;
int i = 10;
int j = 20;
cout << "i, j= " << i << ", " << j << ".\n";
cout << "Using compiler-generated int swapper: \n";
Swap(i, j);
cout << "Now i, j = " << i << ", " << j << ".\n";
system("pause");
return 0;
}
template<typename Any>
void Swap(Any& a, Any& b)
{
/*
a = a+b;
b = a-b;
a = a-b;
*/
/*
a = a|b;
b = a^b;
a = a^b;
*/
}
注:某些编译器可能不支持template<typename Any>的形式,可以换成template<class Any>的形式,二者效果一样。
Swap方法内的逻辑,自己随手写了两种方法,当然还有其他方法,也不愿深究了。