zoukankan      html  css  js  c++  java
  • 一个FLAG #16# 优先队列

    例子

    #include <iostream>
    #include <queue>
    #include <vector>
    using namespace std;
    
    struct cmp {
        bool operator() (const int a, const int b) const {
            return a % 10 < b % 10;  
        }
    };
    
    int main()
    {
        // 默认的优先队列。数值越大优先级越高 
        priority_queue<int> pq;
        pq.push(2);
        pq.push(123);
        pq.push(1235);
        pq.push(55);
        pq.push(1230);
        cout << pq.top() << endl; pq.pop(); // => 1235
        cout << pq.top() << endl; pq.pop(); // => 1230
        cout << pq.top() << endl; pq.pop(); // => 123
        cout << pq.top() << endl; pq.pop(); // => 55
        cout << pq.top() << endl; pq.pop(); // => 2
        
        cout << pq.top() << endl; pq.pop(); // => 2
        cout << pq.top() << endl; pq.pop(); // => 2
        cout << pq.top() << endl; pq.pop(); // => 2
        
        cout << " ==================================== == =" << endl;
        // 越小的整数优先级越高
        priority_queue<int, vector<int>, greater<int> > pq2; 
        pq2.push(2);
        pq2.push(123);
        pq2.push(55);
        cout << pq2.top() << endl; pq2.pop(); // => 2
        cout << pq2.top() << endl; pq2.pop(); // => 55
        cout << pq2.top() << endl; pq2.pop(); // => 123
        
        cout << " ==================================== == =" << endl;
        // 自定义的优先队列,个位越大的优先级越高 
        priority_queue<int, vector<int>, cmp> pq3;
        pq3.push(2);
        pq3.push(123);
        pq3.push(55);
        cout << pq3.top() << endl; pq3.pop(); // => 55
        cout << pq3.top() << endl; pq3.pop(); // => 123
        cout << pq3.top() << endl; pq3.pop(); // => 2
            
        return 0;
    }
  • 相关阅读:
    黑马java课程2222
    屏幕亮度软件和一些自己必用的软件设置
    ahk保存
    天天洗一次澡还真是好方法
    自动化测试 就这两张图
    python __init__.py用途
    bash检查文件格式
    cygwin中运行命令提示command not found的解决方法
    Python批量插入SQL Server数据库
    怎样去掉FireFox的导入向导
  • 原文地址:https://www.cnblogs.com/xkxf/p/12690174.html
Copyright © 2011-2022 走看看