C++ list链表排序

#include <iostream>
#include <string>
#include<list>
using namespace std;

//list 排序主要使用成员函数  Data.sort(cpmpare); 
typedef struct 
{
    int GoldN;
    int SilverN;
    int BronzeN;
    string Country;

    void setData(int tem1,int tem2,int tem3,string tems) {
        GoldN = tem1;
        SilverN = tem2;
        BronzeN = tem3;
        Country = tems;
    }
    void coutData() {
        cout << Country << endl;
    }
}Olym_Data;


bool cpmpare(Olym_Data a, Olym_Data b)
{
    if (a.GoldN > b.GoldN)
        return true;
    else if (a.GoldN < b.GoldN)
        return false;
    else {
        if (a.SilverN > b.SilverN)
            return true;
        else if (a.SilverN < b.SilverN)
            return false;
        else {
            if (a.BronzeN > b.BronzeN)
                return true;
            else if (a.BronzeN < b.BronzeN)
                return false;
            else {
                if (a.Country > b.Country)
                    return true;
                else
                    return false;
            }
        }
    }
}

int list_Sort()
{
    list<Olym_Data>Data;
    Olym_Data temdata;
    int n;
    string tems;
    int tem1, tem2, tem3;
    Data.clear();
    list<Olym_Data>::iterator it;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> tems >> tem1 >> tem2 >> tem3;
        temdata.setData(tem1, tem2, tem3, tems);
        Data.push_back(temdata);
    }
    Data.sort(cpmpare);
    for (it = Data.begin(); it != Data.end(); it++) {
        it->coutData();
    }
    return 0;
}

 

posted @ 2019-10-16 09:43  博客园—哆啦A梦  阅读(1299)  评论(0)    收藏  举报