priority_queue自定义排序

时间:2025-04-04 08:14:02
方法1: // 按pair的第二个元素降序排列 static bool cmp(pair<int,int> a, pair<int, int> b){ return a.second > b.second; } priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> q(cmp); 方法2: struct cmp1{ bool operator()(pair<int,int> a, pair<int, int> b){ return a.second > b.second; } }; priority_queue<pair<int,int> , vector<pair<int,int>>, cmp1> q; priority_queue<int> q;//默认是从大到小 priority_queue<int, vector<int> ,less<int> >q;//从大到小排序 priority_queue<int, vector<int>, greater<int> >q;//从小到大排序