我想按降序对int
和char
(从一个类)的数组进行排序。这些是学生的姓名和成绩。使用冒泡排序从类中排序2D Char和Int数组?
的类定义为:
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];
Siegester,谢谢。代码完美工作。谢谢你的帮助。 – MacKey