STL 自定义比较器

xiaoxiao2021-03-01  24

struct compare { bool operator()(const pair<string,int>&p1,const pair<string,int>&p2) { return p1.second>p2.second;//second为value ,从大到小的顺序 } } struct compare1 { bool operator()(const pair<string,int>&p1,const pair<string,int>&p2) { return p1.second<p2.second;//second为value ,从小到大的顺序 } } //排序 sort(vec.begin(),vec.end(),compare());//调用自己的比较器函数 struct CMP { public: bool operator() (const int a,const int b) { if(a>b) { return true; // 从大到小 }else { return false; } } }; bool CMP1 (const int a,const int b) { if(a<b) { return true; // 从小到大 }else { return false; } }
转载请注明原文地址: https://www.6miu.com/read-3650068.html

最新回复(0)