【问题标题】:Sorting a 2D Char and Int Array from class using bubble sort?使用冒泡排序从类中对 2D Char 和 Int 数组进行排序?
【发布时间】:2012-11-24 18:26:30
【问题描述】:

我正在尝试按降序对intchars(来自一个类)的数组进行排序。这些是学生姓名和成绩。

类定义为:

class Student {
public:
    char name[20];
    int grades;
};

numCount是记录数的增量值。

void bubble_sort(Student theResults[], int numCount)
{
  bool swapped = true;
  while(swapped)
  {
    swapped = false;
    for(int i=1;i<numCount;i++)
    {
      if(theResults[i-1].grades < theResults[i].grades)
      {
        int tempHold = theResults[i-1].grades;
        theResults[i-1].grades = theResults[i].grades;
        theResults[i].grades = tempHold;
        swapped = true;
      }
    }
  }

我遇到的问题是 int 值(等级)在循环后正确排序,但难以正确分配名称以匹配等级。

我使用了以下代码,但它不起作用,因为它为学生显示了不正确的成绩。

char* title_temp = theResults[i-1].name;
theResults[i-1].name[20] = theResults[i].name[20];
theResults[i].name[20] = title_temp[20];

【问题讨论】:

    标签: c++ class multidimensional-array bubble-sort


    【解决方案1】:

    我认为你的问题在这里:

    if(theResults[i-1].grades < theResults[i].grades)
    {
        int tempHold = theResults[i-1].grades;
    
        theResults[i-1].grades = theResults[i].grades;
    
        theResults[i].grades = tempHold;
    
        swapped = true;
    }
    

    你真正想做的是

    if(theResults[i-1].grades < theResults[i].grades)
    {
        Student tempHold = theResults[i-1];
    
        theResults[i-1] = theResults[i];
    
        theResults[i] = tempHold;
    
        swapped = true;
    }
    

    在您更改的只是成绩值而不是名称之前,这将切换整个 Student 对象并应该产生您正在寻找的输出

    【讨论】:

    • 西格斯特,谢谢。代码运行良好。感谢您的帮助。
    【解决方案2】:

    您必须使用循环一次复制整个 char 块,每个元素,或者您可以使用 memcpy。

    你也可以使用你的类的浅拷贝

    void bubble_sort(Student theResults[], int numCount)
    {
    
    
        bool swapped = true;
        while(swapped)
        {
            swapped = false;
            for(int i=1;i<numCount;i++)
            {
                if(theResults[i-1].grades < theResults[i].grades)
                {
                    Student tempHold = theResults[i-1];
    
                    theResults[i-1]= theResults[i];
    
                    theResults[i] = tempHold;
    
                    swapped = true;
                }
            }
        }
    }
    

    【讨论】:

    • Nico,上面的代码也很完美。感谢您的时间和帮助。PS 抱歉只能将 1 标记为正确答案,即使所有三个帖子都是正确的,我还是做了第一篇帖子。对不起。
    【解决方案3】:

    问题是你需要交换对象,分数只需要作为一个key来指导排序,试试这个:

    void bubble_sort(Student theResults[], int numCount)
    {
    
        Student tempHold;
        bool swapped = true;
        while(swapped)
        {
            swapped = false;
            for(int i=1;i<numCount;i++)
            {
                if(theResults[i-1].grades < theResults[i].grades)
                {
                    tempHold = theResults[i-1]; //swap the objects, not just the grades.
    
                    theResults[i-1]= theResults[i];
    
                    theResults[i] = tempHold;
    
                    swapped = true;
                }
            }
        }}
    

    但是,如果你必须复制成员,那么除了交换等级:

    char temp[20];
    strcpy(temp ,theResults[i-1].name);
    strcpy(theResults[i-1].name,theResults[i].name);    
    strcpy(theResults[i].name,temp);
    

    而不是使用

        char* title_temp = theResults[i-1].name; // <-wrong
       theResults[i-1].name[20] = theResults[i].name[20];//20 is invalid index
        theResults[i].name[20] = title_temp[20]; //this is just 1 element out of the whole array
    

    由于许多原因,这是错误的。

    【讨论】:

    • 公理,优秀 :D 也很有魅力。感谢您的帮助和建议。 PS 抱歉只能将 1 标记为正确答案,并且即使所有三个帖子都是正确的,也做了第一个帖子。对不起。
    猜你喜欢
    • 2019-06-14
    • 2020-07-17
    • 2020-05-17
    • 2016-07-25
    • 2018-09-22
    • 2012-11-03
    • 2019-05-10
    • 2017-09-27
    • 2019-04-14
    相关资源
    最近更新 更多