虽然有一点点的局限性,但是这种思维很好,效率很高!
#include "stdafx.h"
#include <iostream>
using namespace std;
void Mothed1(int &x,int &y)
{
int temp = x;
x = y;
y = temp;
}
void Mothed2(int &x,int &y)
{
x = x + y;
y = x - y;
x = x - y;
}
void main()
{
//按引用传值高效方案(牛!)
int i = 2, j = 4;
cout << "i是:" << i << "\tj是:"<<j<<endl;
Mothed1(i,j);
cout << "i是:" << i << "\tj是:"<<j<<endl;
Mothed2(i,j);
cout << "i是:" << i << "\tj是:"<<j<<endl;
//方法2的好处是不用重新定义变量,节省内存!
}