zoukankan      html  css  js  c++  java
  • priority_queue的用法

    #include<iostream>
    #include<queue>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> P;
    struct node{
        int x,y;
        node(){}
        node(int cx,int cy):x(cx),y(cy){}
        friend bool operator<(const node& a,const node& b)
        {
            return a.x > b.x; //由小到大
            //反之, a.x < b.x //由大到小 
        }
        void out()
        {
            cout<<"("<<x<<","<<y<<")"<<endl;
        }
    };
    void test_1()
    {
        priority_queue<LL,vector<LL>,greater<LL> > que;//由小到大出队 
        que.push(1);
        que.push(5);
        que.push(3);
        while(!que.empty())
        {
            LL e=que.top();que.pop();
            cout<<e<<endl;
        }
    }
    void test_2()
    {
        priority_queue<LL> que;//由大到小出队 
        que.push(1);
        que.push(5);
        que.push(3);
        while(!que.empty())
        {
            LL e=que.top();que.pop();
            cout<<e<<endl;
        }
    }
    void test_3()
    {
        priority_queue<P,vector<P>,greater<P> > que;//按first的值由小到大出队 
        //同理 priority_queue<P> 则结果按first的值由大到小出队 
        que.push(P(3,5));
        que.push(P(1,3));
        que.push(P(2,6));
        while(!que.empty())
        {
            P pr=que.top();que.pop();
            cout<<pr.first<<" "<<pr.second<<endl;
        }
    }
    void test_4()
    {
        priority_queue<node> que;
        que.push(node(3,5));
        que.push(node(1,3));
        que.push(node(2,6));
        while(!que.empty())
        {
            node e=que.top();que.pop();
            e.out();
        }
    }
    int main()
    {
        test_4();
        return 0;
    }
  • 相关阅读:
    jquery学会的
    oracle技巧-持续更新
    c语言技巧--长期更新
    2019暑假集训 最大子树和
    2019暑假集训 细胞分裂
    2019暑假集训 金明的预算方案
    2019暑假集训 能量项链
    2019暑假集训 神经网络
    0023-特殊的方程
    0022-并联电阻
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4927411.html
Copyright © 2011-2022 走看看