#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; }
来源:博客园
作者:汁虫
链接:https://www.cnblogs.com/huafan/p/11616550.html