题解 | 小白鼠排队
#include <bits/stdc++.h> using namespace std; struct Mouse{ int weight; string name; bool operator<(Mouse a){ return weight>a.weight; } }; int main(){ int n; while(cin>>n){ Mouse a[n]; for(int i=0;i<n;i++){ cin>>a[i].weight>>a[i].name; } sort(a,a+n); for(auto x:a){ cout<<x.name<<endl; } } }
自定义比较方法,我这里试了一下sort的内在逻辑是不是基于小于号,这里发现确实是的,就不用写一个专门的参数了