当前位置 博文首页 > Aaron_Yang:用sort 对结构体排序

    Aaron_Yang:用sort 对结构体排序

    作者:[db:作者] 时间:2021-07-17 15:49

    思路:

    先定义一个你想要的结构体,然后再自定义一个cmp函数(根据需要来进行大小比较的判断),最后在main函数中通过sort(xxx,xxx+n,cmp)来进行判断即可

    实例:

     struct people
    {
        string name;
        string num;
        int score;
    };
    bool cmp(people p1, people p2)
    {
        if (p1.score > p2.score)
            return true;
        else
            return false;
    }
    //从大到小排序
    sort(p,p+n,cmp);
    
    cs
    下一篇:没有了