具体请参看这里:
http://blog.csdn.net/morewindows/article/details/7370155
http://blog.csdn.net/hackbuteer1/article/details/7462447
#include <assert.h>
#include <iostream>
using std::cout;
using std::endl;
//在交换过程中,没有改变指针的值,
//而是改变指针所指向的内容
void Permutation(char* pStr, char* pBegin)
{
assert(pStr && pBegin);
if(*pBegin == '\0')
{
printf("%s\n",pStr);
}
else
{
for(char* pCh = pBegin; *pCh != '\0';)
{
std::swap(*pBegin,*pCh);//交换a和b的值
Permutation(pStr, pBegin+1);
std::swap(*pBegin,*pCh);
pCh++;
}
}
}
int main()
{
char str[] = "abc";
cout<<"str pointer to:"<<*str<<endl;
printf("str=%s\n",str);
Permutation(str,str);//传递指针
system("pause");
return 0;
}
结果:
str pointer to:a
str=abc
abc
acb
bac
bca
cba
cab
请按任意键继续. . .
Process returned 0 (0x0) execution time : 2.062 s
Press any key to continue.
====END====