当前位置 博文首页 > Aaron_Yang:用sort 对结构体排序
先定义一个你想要的结构体,然后再自定义一个cmp函数(根据需要来进行大小比较的判断),最后在main函数中通过sort(xxx,xxx+n,cmp)来进行判断即可
struct people
{
string name;
string num;
int score;
};
bool cmp(people p1, people p2)
{
if (p1.score > p2.score)
return true;
else
return false;
}
//从大到小排序
sort(p,p+n,cmp);
cs