我有一个数组包含来自输入的 chars
,另一个数组包含指向第一个数组中相应 chars
的指针。这部分进展顺利。
但是我想对 char**
数组(指针数组)进行冒泡排序,这样原始数组保持不变但出了点问题(文本未排序)。
EDIT: Please discuss only the sorting algorithm
char tab[200];//array of chars
char** t = new char*[tabLength];
//SOMETHING
....
....
int n = tabLength;//length of tab(length of the word it contains)
//TILL HERE EVERYTHING IS FINE -----------------------------------
//bubble sorting below
do{
for(int i = 0; i < n -1; i++){
if(*t[i] > *t[i+1]){
char* x = t[i];
t[i] = t[i+1];
t[i+1] = x;
}
n--;
}
}while(n>1);
cout<<"sorted input";
for(int i = 0; i < tabLength; i++)
cout<<t[i];
cout<<endl;
cout<<"original"<<tab<<endl;
最佳答案
确保打印出指针指向的值:
for(int i = 0; i < tabLength; i++)
cout << *t[i];
关于c++ - 如何对指向另一个字符数组的指针数组进行冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15452364/