美文网首页
priority_queue

priority_queue

作者: shasha075 | 来源:发表于2022-08-17 23:15 被阅读0次

    using namespace std;

    struct Node {
    int x;
    int y;
    Node(int xx, int yy) : x(xx), y(yy) {}
    // priority_queue默认大根堆,采用less<int>,与map从小到大不一样;优先队列默认按照最大优先级,所以需要重构<
    // 该例子,按照X从大到小排序,如果x相同,则按照y从小到大排列
    bool operator < (const Node &b) const
    {
    if (x == b.x) {
    return y > b.y; // 以b为对象,从小到大;
    }
    return x < b.x; // 以b为对象,顺着从大到小;
    }
    };

    int main()
    {
    priority_queue<Node> que;
    Node node1 = {1, 5};
    Node node2 = {1, 2};
    Node node3 = {3, 6};
    que.push(node1);
    que.push(node2);
    que.push(node3);

    while (que.empty() != true) {
        cout<<que.top().x<<que.top().y<<endl;
        que.pop();
    }
    
    return 0;
    

    }

    相关文章

      网友评论

          本文标题:priority_queue

          本文链接:https://www.haomeiwen.com/subject/gzbsgrtx.html