sort()函数默认升序排序 可以通过自定义来对指定元素进行指定顺序排序
方法1:在类外部即全局 定义比较函数
bool cmp(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //从小到大排序
}
sort(r.begin(), r.end(), cmp);
方法2:重载类的比较运算符
bool operator<(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //从小到大排序
}
sort(r.begin(), r.end());
04:30:00 - 05:30:00
07:10:58 - 07:10:59
09:00:00 - 13:00:00
19:00:00 - 23:59:59
1
2
3
4
天梯赛一道题就用到了自定义sort函数
![](https://i-blog.csdnimg.cn/blog_migrate/98da3e767f667a95bdedf1218aeada96.png)
![](https://i-blog.csdnimg.cn/blog_migrate/147df6829a016aa2dde8fa7fb2ca3a2e.png)
参考的题解如下
![](https://i-blog.csdnimg.cn/blog_migrate/1f5f49bcf0e5338e77935f23c60f57e0.png)