004 list::sort

匿名 (未验证) 提交于 2019-12-03 00:13:02

 

#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; }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!