【心情】Priority_queue容器的用法

所给的代码最顶端是最小的元素
要改为最顶端是最大的则只需把

friend  bool operator<(Node a, Node b) { return  a.val > b.val; } 


改成

friend  bool operator<(Node a, Node b) { return  a.val < b.val; } 


即可。

#include <iostream>
#include <queue>
using namespace std;

struct Node
{
    int adj;
    int val;
    friend  bool operator<(Node a, Node b) { return  a.val > b.val; }
};

priority_queue<Node>Q;
Node temp;

int main()
{
    temp.val = 2;
    Q.push(temp);
    temp.val = 29;
    Q.push(temp);
    temp.val = 67;
    Q.push(temp);
    temp.val = 17;
    Q.push(temp);
    while (!Q.empty())
    {
        temp = Q.top();
        printf("%d\n", temp.val);
        Q.pop();
    }
    return 0;
}
posted @ 2017-10-06 19:23  AWCXV  阅读(98)  评论(0)    收藏  举报