我们以C++语言作为演示,除了引用是C所不支持的,其他都可以。
方法一:使用指针+第三方变量
#include <iostream>
using namespace std;
void exchange(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(&num1, &num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
return 0;
}
- #include <iostream>
- using namespace std;
- void exchange(int *a, int *b)
- {
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- int main(void)
- {
- int num1 = 2;
- int num2 = 3;
- exchange(&num1, &num2);
- cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
- return 0;
- }
#include <iostream>
using namespace std;
void exchange(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(&num1, &num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
return 0;
}
解释:将num1和num2的地址传递给exchange()函数,然后让该函数直接对num1和num2中的内容做交换。如图:



方法二:使用指针+按位移或
#include <iostream>
using namespace std;
void exchange(int *a, int *b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(&num1, &num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
return 0;
}
- #include <iostream>
- using namespace std;
- void exchange(int *a, int *b)
- {
- *a = *a ^ *b;
- *b = *a ^ *b;
- *a = *a ^ *b;
- }
- int main(void)
- {
- int num1 = 2;
- int num2 = 3;
- exchange(&num1, &num2);
- cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
- return 0;
- }
#include <iostream>
using namespace std;
void exchange(int *a, int *b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(&num1, &num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
return 0;
}
按位移或:相同为0,不同为1。
真值表如图:
a |
b |
结果 |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |

方法三:使用引用(C++特有,C语言不可用)+第三方变量
#include <iostream>
using namespace std;
void exchange(int &num1, int &num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(num1, num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
}
- #include <iostream>
- using namespace std;
- void exchange(int &num1, int &num2)
- {
- int temp = num1;
- num1 = num2;
- num2 = temp;
- }
- int main(void)
- {
- int num1 = 2;
- int num2 = 3;
- exchange(num1, num2);
- cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
- }
#include <iostream>
using namespace std;
void exchange(int &num1, int &num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(num1, num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
}
第三方变量的作用的方法一中的是一样的,下面来解释下C++中的引用。
C++中的引用的基本原理还是指针变量。使用&num1相当于为num1变量取一个别名。在函数exchange()中,使用num1就相当于用了它的别名,直接对其地址操作。所以在函数中改变了num1的值,它本身的值也是会变的。
方法四:使用引用+按位异或
#include <iostream>
using namespace std;
void exchange(int &num1, int &num2)
{
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(num1, num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
}
- #include <iostream>
- using namespace std;
- void exchange(int &num1, int &num2)
- {
- num1 = num1 ^ num2;
- num2 = num1 ^ num2;
- num1 = num1 ^ num2;
- }
- int main(void)
- {
- int num1 = 2;
- int num2 = 3;
- exchange(num1, num2);
- cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
- }
#include <iostream>
using namespace std;
void exchange(int &num1, int &num2)
{
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
}
int main(void)
{
int num1 = 2;
int num2 = 3;
exchange(num1, num2);
cout<<"num1: "<<num1<<"\nnum2: "<<num2<<endl;
}
引用和按位异或在前面都讲过了。这里不再重复。以上就是使用函数交换两个数的四种常见方法。