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;//值越小优先级大
    }
};

而对于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>()//从小到大

猜你喜欢

转载自www.cnblogs.com/kangdong/p/9010263.html