swap 函数的四种写法 (1)经典型 --- 嫁衣法 void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
} (2)经典型改进版 --- 泛型法 void swap(void *a, void *b, size_t size)
{
unsigned char *p = (unsigned char *)a;
unsigned char *q = (unsigned char *)b;
unsigned char temp;
while(size--)
{
temp = *p;
*p = *q;
*q = temp;
p ++;
q ++;
}
} (3)取巧型 --- 赋值法 void swap(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
} (4)诡异型 --- 逻辑运算法 void swap(int *a, int *b)
{
*a = *a ^ *b;
*b = *b ^ *a;
*a = *a ^ *b;
}