优先权队列使用示例 C++ STL

版权声明:转载请注明出处及原文地址。 https://blog.csdn.net/zl1085372438/article/details/82623098
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
using namespace std;

struct DATA
{
    int x,y;
    DATA(int xx,int yy)
    {
        x=xx;
        y=yy;
    }
};
typedef struct DATA DATA;


struct cmp
{
    bool operator()(DATA a,DATA b)    //a代表小的数,b代表大的数,a>b代表小数优先级大,a<b代表小数优先级小。
    {
        return a.x>b.x;     //表示按照x的值由小到大排序。(为什么这边由小到大,因为a代表小的数,b代表大的数,前面的大于后面的,表示小的数优先级大,这边的大于和小于号代表的是谁的优先级大。
        //return a.y>b.y;   //表示按照y的值由小到大排序。
        //return a.x<b.x;
    }
};

int main()
{
    priority_queue<DATA,vector<DATA>,cmp> Q;
    for(int i=0;i<10;i++)
    {
        DATA tmp(10-i,i);
        Q.push(tmp);
    }
    while(Q.size()>0)
    {
        printf("%d %d\n",Q.top().x,Q.top().y);
        Q.pop();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zl1085372438/article/details/82623098
今日推荐