C ++使用qsort对类数组进行排序

时间:2017-04-08 21:16:49

标签: c++ class

C ++: 我试图对一些普通媒体存储在课堂上的学生进行排序。

只有qsort,不要告诉我std :: sort,谢谢!

Qsort比较功能:

int cmp(Student *a, Student *b) {
    return (int)(((Student *)b)->get_media() - ((Student *)a)->get_media());
}

qsort电话:

qsort(&tab, (size_t)n, sizeof(tab), (int(*)(const void*, const void*))cmp);

没有编译器错误,但它不会排序。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Student {
private:
    char name[20];
    char surname[20];
    int *marks;
    int group;
    float avg_mark;
public:
    Student()
    {
        char na[20], sur[20];
        int group;
        cout << "\nEnter name: ";
        cin >> na;
        cout << "\nEnter surname: ";
        cin >> sur;
        cout << "\nEnter group: ";
        cin >> group;
        init(na, sur, group);
    }
    ~Student()
    {
        cout << "\ndestructor";
        delete []marks;
    }
    void init(char *n, char *p, int gr)
    {
        strcpy(name, n);
        strcpy(surname, p);
        group = gr;
        marks = new int[6];
        for (int i = 0; i < 6; i++)
        {
            cout << "\nEnter mark " << i + 1 << ": ";
            cin >> *(marks + i);
        }
        avg_mark = media();
    }
    float media()
    {
        int s = 0;
        for (int i = 0; i < 6; i++)
            s += marks[i];
        return ((float)s / 6);
    }
    void set_name(char *n)
    {
        strcpy(name, n);
    }
    char* get_name()
    {
        return name;
    }
    void set_surname(char *p)
    {
        strcpy(name, p);
    }
    char* get_surname()
    {
        return surname;
    }
    int get_group()
    {
        return group;
    }
    float get_media()
    {
        return avg_mark;
    }
};

int cmp(Student *a, Student *b);

int comparator(void *a, void *b) {
    return (int)(((Student *)b)->get_media() - ((Student *)a)->get_media());
}



void main(void)
{
    int n;
    cout << "\nEnter n: ";
    cin >> n;
    Student *tab = new Student[n];
    for (int i = 0; i < n; i++)
        cout << i + 1 << ". " << tab[i].get_name() << " " << tab[i].get_surname() << " Group:" << tab[i].get_group() << " Average mark: " << tab[i].get_media() << endl;
    //qsort(&tab[0], (size_t)n, sizeof(tab), (int*)cmp);
    cout << endl;
    qsort(&tab, (size_t)n, sizeof(tab), (int(*)(const void*, const void*))cmp);
    for (int i = 0; i < n; i++)
        cout << i + 1 << ". " << tab[i].get_name() << " " << tab[i].get_surname() << " Group:" << tab[i].get_group() << " Average mark: " << tab[i].get_media() << endl;
    cin.ignore();
    cin.get();
}

int cmp(Student *a, Student *b) {
    return (int)(((Student *)b)->get_media() - ((Student *)a)->get_media());
}

1 个答案:

答案 0 :(得分:1)

qsort(&tab, (size_t)n, sizeof(tab), (int(*)(const void*, const void*))cmp);

&tab是指针tab的地址。您想传递数组的第一个元素的地址。这是&tab[0]或简称tab

此外,您需要传递Student对象的大小,而不是指针的大小。因此,将sizeof(tab)更改为sizeof(Student)sizeof(*tab)。所以电话应该是这样的:

qsort(tab, (size_t)n, sizeof(*tab), (int(*)(const void*, const void*))cmp);