结构体排序-2020--11--29


一、使用步骤

1.先定义结构体数组

struct stu{
    
    
	string id;
	int score;
}stus[1010];

2.写排序函数

由大到小或由小到大

代码如下:

bool cmp(stu a,stu b)
{
    
    
	return a.score>a.score;
}

3.排序

在main函数中,使用sort(某些题目可能需要用到stable_sort)

sort(stus,stus+n,cmp);

当使用stable_sort时,若有两个值相等,则排序后不会改变它们的相对位置。


猜你喜欢

转载自blog.csdn.net/p15008340649/article/details/110324762