参考链接: C 程序使用引用调用按循环顺序交换数字
#include <iostream>
using namespace std;
void swap1(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
void swap2(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
void swap3(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<"Before swap: a="<<a<<" b="<<b<<endl;
swap1(a,b);
cout<<"swap1 : a="<<a<<" b="<<b<<endl;
cout<<"Before swap: a="<<a<<" b="<<b<<endl;
swap2(a,b);
cout<<"swap2 : a="<<a<<" b="<<b<<endl;
cout<<"Before swap: a="<<a<<" b="<<b<<endl;
swap3(&a,&b);
cout<<"swap3 : a="<<a<<" b="<<b<<endl;
}