如何对字符串字符串数组的每个字符串进行排序
How to sort each character string of character string array
我想对每个字符串数组进行排序,这是我尝试的代码。
#include <iostream>
#include <algorithm>
void _sort_word(char *str)
{
int len = strlen(str);
std::sort(str,str+len); // program get stuck here.
}
int main()
{
char *str[] = {"hello", "world"};
for(int i=0;i<2;i++){
_sort_word(str[i]);
cout << str[i] << "n";
}
}
我想知道sort(str,str+len);
在这里是有效的语句,如果没有,该怎么办?
C 中的所有字符串文字中的首先具有常数字符数组的类型。因此,正确的数组声明看起来像
const char *str[] = {"hello", "world"};
^^^^^
因此,阵列的元素指向的字符串文字是不变的。
您应该至少声明一个二维数组。
这是一个指示的程序
#include <iostream>
#include <algorithm>
#include <cstring>
void sort_word( char *s )
{
size_t l = std::strlen( s );
std::sort( s, s + l );
}
int main()
{
char str[][6] = { "hello", "world" };
for ( auto &s : str ) sort_word( s );
for ( auto &s : str ) std::cout << s << std::endl;
return 0;
}
其输出是
ehllo
dlorw
如果您的编译器不支持基于语句的范围,则可以编写
for ( size_t i = 0; i < sizeof( str ) / sizeof( *str ); i++ ) sort_word( str[i] );
相关文章:
- 对字符串进行排序时,在c++中处理sort()
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 按字母顺序对字符串中的字母进行排序,而无需使用内置的 sort()
- 字符串上的合并排序上的 Seg 错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 如何在数组 c++ 中对字符串进行排序
- 提高从排序字符串中获取字母顺序的顺序复杂性
- 尝试使用C++中的模板进行BST排序,但在使用随机字符串进行测试时不起作用
- 如何根据第二列/第三列等对字符串向量进行排序?
- 如何对向量进行排序<浮点数,字符串>而不考虑字符串
- C ++中的字符串排序,也没有在第5个元素正确排序
- C++:使用 LSD 基数排序字符串排序崩溃
- C 样式的字符串排序与排序和 qsort
- 自定义字符串排序
- 编号和字符串排序Al
- 使用STL对子字符串排序
- 如何在c++中按字母顺序对标准输入字符串排序
- c++向量字符串排序不工作
- SQLite 的不区分大小写的 UTF-8 字符串排序规则 (C/C++)