#include <iostream>
using namespace std;
void swap(int &a, int &b){
int t ; //中间变量t就像网购中支付宝的作用
t = a; //买家a将钱先打到支付宝t中
a = b; //卖家b把货发给买家a
b = t; //买家a收到货并确认收货后,支付宝t将钱打给卖家
}
int main()
{
int a = 6;
int b = 9;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
swap(a,b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
}
输出结果为: