This question already has answers here:
How do I sort a vector of pairs based on the second element of the pair?
                                
                                    (7个答案)
                                
                        
                                4年前关闭。
            
                    
在Google上搜索了一段时间后,我无法找到专门回答此问题的答案。如何对其中具有某些自定义类型的C ++ STL对进行反向排序

pair <long long, pair< int, int > > p[MAX];

最佳答案

#include<bits/stdc++.h>
using namespace std;

const int MAX = 1e5 + 5;
typedif pair <long long, pair< int, int > > pll;
pll p[MAX];

//custom sort function can be customized
bool SortByWeight(pll x, pll y){
    return x.first > y.first;
}

int main(){
    //enter size
    cin>>szie;
    //Ex: p = [(10101, (2,3)), (129334, (4, 7))....]
    sort(p, p+size, SortByWeight)
}


定制功能可以针对定制类型进行定制。对于ex类,我们可以使用class_obj.variable_name等


来源我的博客mypynotes

10-08 14:31