zoukankan      html  css  js  c++  java
  • 优先队列的使用

    优先队列 和 普通队列一样, 队尾插入,队头删除.

    不一样的是, 优先队列出队时, 是按照一定的规则 出来,比如最大,最小的.

    元素的比较规则默认为按元素的值的由大到小排序;当然,可以重载“<”操作符来重新定义比较规则;

    优先队列包含入队push()(插入元素),出队pop()(删除元素),读取队头元素top(),判断队列是否为空empty()和读取队列元素数量size()等方法;

    通过重载运算符 < 定义比较规则的代码:

    #include <stdio.h>
    #include <iostream>
    #include <queue>
    using namespace std;
    
    struct Node
    {
        int x, y;
        bool operator < (const Node &a) const
        {
            return y < a.y; //按照 y 从大到小排列, 反之从小到大
        }
    };
    
    int main()
    {
        Node node[5];
        node[1].x = 5; node[1].y = 1;
        node[2].x = 4; node[2].y = 2;
        node[3].x = 3; node[3].y = 3;
        node[4].x = 2; node[4].y = 4;
    
        priority_queue<Node> q;
        q.push(node[2]);
        q.push(node[1]);
        q.push(node[4]);
        q.push(node[3]);
        while(!q.empty())
        {
            printf("x = %d, y = %d
    ", q.top().x, q.top().y);
            q.pop();
        }
        return 0;
    }
    
  • 相关阅读:
    POJ 1987 Distance Statistics
    mongo 查询
    图解SSH原理_20190613
    Mongo 备份
    地理空间数据
    fiddler 4 抓取 https 设置
    2、动态元素的定位
    1、selenium 8大元素定位方式
    1、Fiddler 打断点 bpu
    2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null
  • 原文地址:https://www.cnblogs.com/tenlee/p/4571921.html
Copyright © 2011-2022 走看看