结构体用sort排序

#include <iostream>

#include <algorithm>
using namespace std;
 struct  stu{
    char name[20];
    int  ar;
};
 bool map(struct stu a,struct stu b){
     if(a.ar<b.ar) return true;
     else  return false;
 }
int main()
{
  stu stu[100];
  int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>stu[i].name>>stu[i].ar;
    }
  sort(stu,stu+n,map);
    cout<<stu[k-1].name<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/yusuph/p/13307882.html