C++中交换排序的一个问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 19:23:20
是quicksort中的一个问题。
void swap(string* pStr[], int first, int second) {
string* temp=pStr[first];
pStr[first]=pStr[second];
pStr[second]=temp;
}

我的问题是:
temp在这里是不是一个临时存储,并且指向数组first指针的指针?
pStr[first]=pStr[second];这个不理解,是指两个指针相同吗?

实际上就是交换pStr[first]和pStr[second]指针,temp是临时指针
先让temp和pStr[first]指向同一个变量,然后pStr[first]指向pStr[second]所指变量,然后pStr[second]指向temp所指变量(就是pStr[first]的初试值)

temp是临时存储,指向指针数组pStr中的pStr[first]这个指针所指向的地址。
pStr[first]=pStr[second];可以理解为设两个指针相同,其实质是让pStr[second]这个指针指向了pStr[first]这个指针所指向的地址。
最后完成了pStr[second]和pStr[first]这两个指针指向地址的交换。