我有一个数组包含来自输入的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;
答案 0 :(得分:1)
确保打印出指针指向的值:
for(int i = 0; i < tabLength; i++)
cout << *t[i];
答案 1 :(得分:0)
我只想使用标准库中已有的功能:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string original;
std::getline(std::cin, original);
std::cout << '|' << original << "|\n";
std::string sorted = original;
std::sort(std::begin(sorted), std::end(sorted));
std::cout << "Sorted : " << sorted << '\n';
std::cout << "Original: " << original << '\n';
}
试运行:
|Hello world, how are you doing today?| Sorted : ,?Haadddeeghilllnoooooorrtuwwyy Original: Hello world, how are you doing today?