如何使用快速排序对字符串数组进行排序
How to use Quick Sort to sort an array of strings
我想使用快速排序(std::qsort(arg,arg,arg))
而不是(std::sort(arg,arg,arg))
来对字符串数组进行排序s[ ]
。
那么我还需要在以下代码中更改什么。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while (t--) {
char x[1010];
string s[1010];
scanf("%s", x);
int len = strlen(x);
s[len] = "";
for(int i = len - 1; i >= 0; --i) {
s[i] = x[i];
s[i] += s[i+1];
}
// s[] now contains len number of strings
sort(s, s + len); // So its here where I want to use qsort
}
return 0;
}
我需要传递给qsort(;;)
的所有参数。和如何编写用于快速排序的 cmp() 函数。
您需要创建一个比较函数来比较 2 个字符串,并将其用作 qsort 中的最后一个参数。在此处阅读有关该功能的更多信息http://www.cplusplus.com/reference/cstdlib/qsort/?kw=qsort
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 合并排序不排序自创建数组类 c++
- 带有枚举方向/类型的气泡排序结构数组
- 为什么指针在对二维数组进行排序时无法正常工作?
- 排序并行数组
- 如何在数组 c++ 中对字符串进行排序
- 为什么在此排序算法实现中,向量明显比数组慢?
- 如何对不在数组中的变量进行排序?
- C++数组与向量排序(在我的情况下,向量比数组慢~2.5倍(无优化))
- 按字母顺序对结构内数组变量中的名称进行排序
- 使用排序函数 c++ 对字符数组进行排序
- 使用 std::sort 对二维 c 数组进行排序
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 使用 std::sort 对 C 样式的 2D 数组进行部分排序
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 对 0 、1 和 2 的数组进行排序