如何冒泡排序数组的指针到另一个数组的字符
How to bubble sort array of pointers to the another array of chars
我有一个数组包含输入的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];
我将简单地使用标准库中已有的功能:
#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';
}
测试运行:<>之前世界你好,你今天过得怎么样?|排序:,?Haadddeeghilllnoooooorrtuwwyy你好,世界,你今天过得怎么样?
相关文章:
- 在c++中为double类型的数组创建一个unique_ptr
- 从数组构造一个跨度<shared_ptr<T>><shared_ptr<void>>
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- 为什么我的数组输出一个不在其中的元素
- 数组 - 最后一个索引
- 将数组作为一个参数从一个函数传递到C 中的另一个函数
- 从字符串数组创建一个字符数组
- 如何在C 中的小写中将文本从数组的一个维度传递到另一个维度
- 在对结构数组的一个成员进行排序后,移动其成员的其余部分
- 为什么数组在一个文件中打印,但在发送到另一个文件时却缺少数字
- C++,为什么多维数组的一个元素的增加似乎增加了另一个元素
- 将数组的一个元素与整个数组元素进行比较
- 如何将数组从一个函数传递到另一个函数
- 查找最大和连续子数组-另一个版本
- 使用for循环将char数组的一个元素复制到另一个元素
- C++如何将变量char数组从一个函数传递到另一个函数
- 数组 ->另一个空的 int 数组混淆
- 用另一个字符数组替换一个字符数组