sort函数排序
sort(it.begin().it.end(),cmp);
三个参数:begin,end,排序方式
排序区间:前闭后开;
排序方式:默认从小到大:
sort(it.begin().it.end(),less<type>());
从大到小应改为:
sort(it.begin().it.end(),greater<type>());
缺点:不能对结构体和自定义数据结构排序,需要另写排序函数。
自定义排序函数:
sort(it.begin().it.end(),cmp);//cmp是函数名,返回类型应是bool类型
自定义排序结构体:
sort(it.begin().it.end(),Cmp());//cmp是结构体名
priority_queue优先队列排序
priority_queue<type,容器<type>,cmp()>;
三个参数:数据类型,容器类型(默认vector),排序方式;
排序方式默认大顶堆:
priority_queue<type,容器<type>,less<type> >;
小顶堆应改为:
priority_queue<type,容器<type>,greater<type> >;
区别
sort默认的less是升序;而优先队列默认的less是降序;
这是因为,优先队列的top()获取的是队列尾部元素,pop()也是弹出的队列尾部元素,所以导致,从优先队列读取元素时,和sort的排序形式相反。