关于指针交换的操作
最近重新研究了关于如何通过指针来交换两个值,以及如何交换指针的问题,有了小收获。先上几个函数:
void swap1(int** p1, int** p2) {
//交换指针
int* temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void swap2(int* p1, int* p2)
{
//函数内交换指针,但无法改变函数外的地址
int* temp;
temp = p1;
p1 = p2;
p2 = temp;
}
void swap3(int* p1, int* p2) {
//交换指针指向的值
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void swap4(int& p1, i