
展开全部
这样可以不?
#include <iostream>
using namespace std;
template<class T>
void swap(T* a, T* b)
{
T temp = *a;
*a = *b;
*b = temp;
}
void main()
{
int n1 = 3;
int n2 = 4;
swap(&n1, &n2);
cout << n1 << " " << n2 << endl;
float f1 = 3.4;
float f2 = 4.3;
swap(&f1, &f2);
cout << f1 << " " << f2 << endl;
double d1 = 3.4;
double d2 = 4.3;
swap(&d1, &d2);
cout << d1 << " " << d2 << endl;
}
#include <iostream>
using namespace std;
template<class T>
void swap(T* a, T* b)
{
T temp = *a;
*a = *b;
*b = temp;
}
void main()
{
int n1 = 3;
int n2 = 4;
swap(&n1, &n2);
cout << n1 << " " << n2 << endl;
float f1 = 3.4;
float f2 = 4.3;
swap(&f1, &f2);
cout << f1 << " " << f2 << endl;
double d1 = 3.4;
double d2 = 4.3;
swap(&d1, &d2);
cout << d1 << " " << d2 << endl;
}
追问
可以运行。但是好像不对
追答
还有什么需要修改的?利用了函数模版的,能交换int,float,double类型
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |