容器排序,从大到小
经常会忘记排序的比较函数,是从大到小排序的,还是从小到大排序的。
这里是一个排序函数的示例。不记得的时候经常来查看。
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; struct tagZxgBSInfo { short shtMarket; string sCode; string name; //股票名称 double zdf; //涨跌幅 }; bool compareT1(const tagZxgBSInfo &A, const tagZxgBSInfo &B) { return A.zdf > B.zdf;//从大到小排序 } int main() { tagZxgBSInfo A; tagZxgBSInfo B; tagZxgBSInfo C; A.zdf = 1.2f; B.zdf = 3.6f; C.zdf = 2.2f; std::vector<tagZxgBSInfo> vecZxg; vecZxg.push_back(A); vecZxg.push_back(B); vecZxg.push_back(C); std::sort(vecZxg.begin(), vecZxg.end(),compareT1); cout<<"|0:"<< vecZxg[0].zdf <<"|1:"<< vecZxg[1].zdf <<"|2:"<< vecZxg[2].zdf <<endl; return 0; }
程序执行结果,【从大到小】排序的 |0:3.6|1:2.2|2:1.2