void swap(int *x,int *y);
//传址
int main(){
int x,y;
x=1;y=2;
std::cout << " x=" << x << " y=" << y << "\n";
swap(&x,&y);//传址
std::cout << "after swap: x=" << x << " y=" << y << "\n";
return 0;
}
void swap(int *x,int *y){
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
![这里写代码片](https://i-blog.csdnimg.cn/blog_migrate/196c2acd7934ba915132a213901a4d2c.png)
#include <iostream>
void swap(int &x,int &y);
int main(){
int x,y;
x=1;y=2;
std::cout << " x=" << x << " y=" << y << "\n";
swap(x,y);
std::cout << "after swap: x=" << x << " y=" << y << "\n";
return 0;
}
void swap(int &x,int &y){
x ^= y;
y ^= x;
x ^= y;
}