zoukankan      html  css  js  c++  java
  • 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;
    }


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

  • 相关阅读:
    周总结07(2018.1.8-2018.1.13)
    软件工程概论课总结
    第二阶段团队冲刺-seven
    人月神话阅读笔记06
    第二阶段团队冲刺-six
    周总结06(2018.1.1-2018.1.6)
    第二阶段团队冲刺-five
    开发记录06
    开发记录05
    开发记录04
  • 原文地址:https://www.cnblogs.com/weiweiyi/p/5255736.html
Copyright © 2011-2022 走看看