• prority_queue 的用法 实例


    ①默认的队列 int按照从大至小

    priority_queue<int>;


    ②将pair pa按pa.first从小至大排列

    typedef pair<int,int> pa;
    priority_queue<<pa,vector<pa>,greater<pa>> que;


    ③将int数按照从小至大排列

     priority_queue<int,vector<int>,greater<int>> que ;
    ///其中,第二个函数是容器类型,第三个参数是比较函数

    ④将node按照priority的大小从大到小排列 如果是从大到小 应该改成>吧。。

     struct node 
    {
    friend bool operate<()
    { return n1.priority<n2.priority(); }
    int priority;
    int value;
    };
    #include<iostream>
    #include<functional>
    #include<queue>
    using namespace std;
    struct node
    {
        friend bool operator< (node n1, node n2)
        {
            return n1.priority < n2.priority;
        }
        int priority;
        int value;
    };
    int main()
    {
        const int len = 5;
        int i;
          priority_queue<node> qn;
        node b[len];
        b[0].priority = 6; b[0].value = 1; 
        b[1].priority = 9; b[1].value = 5; 
        b[2].priority = 2; b[2].value = 3; 
        b[3].priority = 8; b[3].value = 2; 
        b[4].priority = 1; b[4].value = 4; 
    
        for(i = 0; i < len; i++)
            qn.push(b[i]);
        cout<<"优先级"<<'	'<<""<<endl;
        for(i = 0; i < len; i++)
        {
            cout<<qn.top().priority<<'	'<<qn.top().value<<endl;
            qn.pop();
        }
        return 0;
    }


    最后代码为转,可调试最后一个用法

  • 相关阅读:
    Leetcode 349. Intersection of Two Arrays
    hdu 1016 Prime Ring Problem
    map 树木品种
    油田合并
    函数学习
    Leetcode 103. Binary Tree Zigzag Level Order Traversal
    Leetcode 102. Binary Tree Level Order Traversal
    Leetcode 101. Symmetric Tree
    poj 2524 Ubiquitous Religions(宗教信仰)
    pat 1009. 说反话 (20)
  • 原文地址:https://www.cnblogs.com/weiweiyi/p/5255736.html
Copyright © 2020-2023  润新知