sort和priority_queue的比较函数总结

对于priority_queue来说,,比较函数为(如果不是结构体,直接int,优先队列默认的是值越大优先级越大):

struct st
{
    string str;
    int pr, value,mark ;
    bool operator < (const st&a)const
    {
        if(pr !=a.pr) return pr > a.pr;//大于号为最小堆
        return mark > a.mark;

//(//小于号为最大堆) } };
1 friend bool operator < (node a, node b)
2     {
3         if (a.x == b.x) return a.y > b.y;
4         return a.x < b.x;
5     }

而对于sort来说

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int cmp(int a, int b)
 5 {
 6     return a > b;//从大到小排序
 7 }
 8 int main()
 9 {
10     int a[] = { 2,1,4,3,4,5,1,3 };
11     sort(a, a + 8, cmp);
12     for (int i = 0; i < 8; i++)
13         cout << a[i] << " ";
14     return 0;
15 }

greater<int>()//从大到小

less<int>()//从小到大

原文地址:https://www.cnblogs.com/kangdong/p/9010263.html