用函数模板实现两个数值交换

#include<iostream>
using namespace std;

//用模板实现两个数值交换
template<class T>
void tswap( T *x, T *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

void main()
{
int a = 5,b = 10;
cout << "a,b 的值:" << a << "," << b << endl;
tswap(&a, &b);
cout << "a,b 的值:" << a << "," <<  b << endl;
}

 

posted on 2022-10-26 20:29  进取  阅读(38)  评论(0编辑  收藏  举报