Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对字符数组中的字进行排序_C++_Arrays_Pointers_Character - Fatal编程技术网

C++ 对字符数组中的字进行排序

C++ 对字符数组中的字进行排序,c++,arrays,pointers,character,C++,Arrays,Pointers,Character,对于给定的数组,我需要编写一个函数来按字母顺序对单词进行排序: char strings [][10] = { "hello", "world", "computers", "are", "awesome" }; 我尝试使用插入排序编写函数sortWords,但我认为我的交换函数不起作用: void swap(char *e1, char *e2) { int tmp = *e1; *e1 = *e2; *e2 = tmp; }

对于给定的数组,我需要编写一个函数来按字母顺序对单词进行排序:

char strings [][10] = {
    "hello",
    "world",
    "computers",
    "are",
    "awesome"
};
我尝试使用插入排序编写函数
sortWords
,但我认为我的交换函数不起作用:

void swap(char *e1, char *e2) {
    int tmp = *e1;
    *e1 = *e2;
    *e2 = tmp;
}

void sortWords( char (* words2Darray)[10], unsigned short length ) {
    unsigned int i, curPos;
    char curChars[10];

    for(i = 1; i < length; i++) {
        // Copy the current word into curChars
        strncpy_s(curChars, words2Darray[i], 10);
        curPos = i;

        // Compare and move the word to the correct position
        while(curPos > 0 && strcmp(curChars, words2Darray[i-1]) > 0) {
            swap(words2Darray[curPos], words2Darray[curPos-1]);
            curPos--;
        }
    }
}
void交换(char*e1,char*e2){
int tmp=*e1;
*e1=*e2;
*e2=tmp;
}
void sortWords(char(*words2Darray)[10],无符号短长度){
无符号整数i,curPos;
char-curChars[10];
对于(i=1;i0&&strcmp(curChars,words2Darray[i-1])>0){
交换(words2Darray[curPos],words2Darray[curPos-1]);
curPos--;
}
}
}
我尝试使用本地Windows调试器调试代码,发现
curChars
被正确复制


有人能告诉我我做错了什么,我应该如何着手解决这个问题吗?我不允许在这个问题中使用
std::string
请不要提供完整的解决方案

您不是在交换字符串,而是在交换字符串的第一个字符。如果要按值传递,则需要以下内容:

void swap(char **e1, char **e2) {
    char *tmp = *e1;
    *e1 = *e2;
    *e2 = tmp;
}
然后使用它:

swap(&words2Darray[curPos], &words2Darray[curPos-1]);
或者,您可以保持
交换
调用不变,并使用引用:

void swap(char *&e1, char *&e2) {
    char *tmp = e1;
    e1 = e2;
    e2 = tmp;
}

< P>(我认为是正确的——我的C++是生锈的。我会做一个测试来确定)。

< P>你可以使用<代码> STD::排序< /C> >:< /P>
std::sort(std::begin(strings), std::end(strings), cmp);
这需要一个比较器。我根据strcmp快速制作了一个

查看它

#include <algorithm>
#include <iostream>
#include <cstring>

int main()
{
    char const* strings[] = {
        "hello",
        "world",
        "computers",
        "are",
        "awesome"
    };

    struct { 
        bool operator()(char const* a, char const* b) const {
            return (a && b)? 0 > strcmp(a,b) : a < b;
        }
    } cmp;

    std::sort(std::begin(strings), std::end(strings), cmp);

    for (auto& s : strings)
        std::cout << s << "\n";
}
#包括
#包括
#包括
int main()
{
字符常量*字符串[]={
“你好”,
“世界”,
“计算机”,
“是”,
“太棒了”
};
结构{
布尔运算符()(字符常量*a,字符常量*b)常量{
返回(a&b)?0>strcmp(a,b):astd::cout程序现在无法编译:
错误C2664:'swap':无法从'char[10]转换参数1对'char * & '/c> >你也有其他bug。把指针传递给数组是很奇怪的。C++代码可能是阴险的。STD::更少的比较将两个char参数转换为字符串,比较它们,然后丢弃字符串。这是一个代码行的例子,它具有比预期的性能影响更大的代码。@ ZANL我写最后一行是有原因的。我不打算为
char(&)[]推荐一个自定义比较器只添加了必须提供的交换。如果OP需要连续存储,我将假定他/她将首先掌握C++的这些方面。在所有的情况之前,我的剖析基本上是为了偷懒而有效的。在这种情况下,你会想要更多的性能。我的10美元说这不是这样。一种情况:)@ZanLynx,不管怎样,我改变了它,现在它根本不用
std::string
,多年后有人花时间否决了它。我想那是没有读评论的。哦,好吧。不能删除接受的答案:/我注意到你的“我不允许在这个问题中使用std::string”我刚刚修改了我的答案。干杯