#include <iostream>
#include <cstring>
using namespace std;
void add(int &x,int &y)
{
int temp = x;
x = y;
y = temp;
}
void add(char &x,char &y)
{
char temp = x;
x = y;
y = temp;
}
void add(double &x,double &y)
{
double temp = x;
x = y;
y = temp;
}
void add(string &x,string &y)
{
string temp = x;
x = y;
y = temp;
}
int main()
{
int x=1,y=2;
add(x,y);
cout << "x = " << x << " y = " << y << endl;
char a='a',b='b';
add(a,b);
cout << "a = " << a << " b = " << b << endl;
double m=1.1,n=2.2;
add(m,n);
cout << "m = " << m << " n = " << n << endl;
string i="hello";
string j="world";
add(i,j);
cout << "i = " << i << " j = " << j << endl;
return 0;
}
不同数据类型的数据交换。