zoukankan      html  css  js  c++  java
  • 004 list::sort

    #include "stdafx.h"
    #include <iostream>
    #include <list>
    
    using namespace std;
    
    typedef struct SStud
    {
        int nNumb;
        char szName[20];
        double fMath;
    }DATA;
    
    bool byNumb(const DATA &dMin, const DATA &dCompare)
    {
        if (dMin.nNumb < dCompare.nNumb)
        {
            return true;
        }
        return false;
    }
    
    bool byName(const DATA &dMin, const DATA &dCompare)
    {
        int nResult = strcmp(dMin.szName, dCompare.szName);
        if (nResult < 0)
        {
            return true;
        }
        return false;
    }
    
    void test()
    {
        
        list<SStud> c1;
        list<SStud>::iterator iter;
        DATA d1 = { 1, "asd", 1.1 };
        DATA d2 = { 22, "222", 2.1 };
        DATA d3 = { 3, "333", 3.1 };
        DATA d4 = { 10, "10", 2.5 };
        c1.push_back(d1);
        c1.push_back(d2);
        c1.push_back(d3);
        c1.push_back(d4);
    
        cout << "Before sorting: " << endl;
        iter = c1.begin();
        while (iter != c1.end())
        {
            DATA d = *iter;
            cout << " " << d.nNumb << " " << d.szName << " " << d.fMath << endl;
            ++iter;
        }
    
        cout << "byNumb sorting: " << endl;
        c1.sort(byNumb);
        iter = c1.begin();
        while (iter != c1.end())
        {
            DATA d = *iter;
            cout << " " << d.nNumb << " " << d.szName << " " << d.fMath << endl;
            ++iter;
        }
    
        cout << "byName sorting: " << endl;
        c1.sort(byName);
        iter = c1.begin();
        while (iter != c1.end())
        {
            DATA d = *iter;
            cout << " " << d.nNumb << " " << d.szName << " " << d.fMath << endl;
            ++iter;
        }
    }
    
    int main(int argc, char *argv[], char **envp)
    {
        test();
    
        return 0;
    }
  • 相关阅读:
    Freefilesync-文件夹自动同步
    考研打卡_Day077
    考研打卡_Day076
    考研打卡_Day075
    考研打卡_Day074
    考研打卡_Day073
    考研打卡_Day072
    考研打卡_Day071
    考研打卡_Day070
    考研打卡_Day069
  • 原文地址:https://www.cnblogs.com/huafan/p/11616550.html
Copyright © 2011-2022 走看看