方法一:使用第三个变量交换
int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;
cout << a << b<< endl;
方法二:使用算术运算
int a = 10;
int b = 20;
a = b - a;
b = b - a;
a = b + a;
cout << a << b << endl;
方法三:使用指针地址
int *a,*b;
*a = new int(10);
*b = new int(20); //&a=0x00001000h,&b=0x00001200h
a = (int*)(b-a); //&a=0x00000200h,&b=0x00001200h
b = (int*)(b-a); //&a=0x00000200h,&b=0x00001000h
a = (int*)(b+int(a)); //&a=0x00001200h,&b=0x00001000h
方法四:使用异或运算符
int a = 10;
int b = 20;
a = a^b;
b = a^b;
a = a^b;
cout << a << b << endl;
方法五:使用栈
stack<int> s;
s.push(10);
s.push(20);
a = s.top();
s.pop();
b = s.top();
s.pop();
cout << a << endl;
cout << b << endl;