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

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
       int a[101];
       for(int i=1;i<=10;i++) a[i]=i;
       sort(a+1,a+1+10,greater<int>() );
       for(int i=1;i<=10;i++)  cout<<a[i]<<"  ";cout<<endl; // cong da dao xiao
       
    cout<<"------------------"<<endl;
    
        priority_queue<int> que; // 默认   最大之优先
        for(int i=1;i<=10;i++)  que.push(i);
        while(!que.empty())   { cout<<que.top()<<" ";  que.pop();  } cout<<endl;
        
        priority_queue<int,vector<int>,greater<int> >que1;//注意“>>”会被认为错误,这是右移运算符,所以这里用空格号隔开
        for(int i=1;i<=10;i++)  que1.push(i);
        while(!que1.empty()) { cout<<que1.top()<<" "; que1.pop();  } cout<<endl;
        
        priority_queue<int,vector<int>,less<int> >que2;////最大值优先
        for(int i=1;i<=10;i++)  que2.push(i);
        while(!que2.empty()) { cout<<que2.top()<<" "; que2.pop();  } cout<<endl;
    }
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5;
    int a[maxn];
    int b[maxn];
    struct NODE{
        int x;
        int y;
        bool operator < (const NODE &a) const
        {
            if(x==a.x) return y>a.y;// zui xiao zhi you xian
            return x>a.x;//最小值优先
        }
    };
    int main()
    {
       for(int i=1;i<=10;i++) a[i]=i;
       for(int i=1;i<=10;i++) b[i]=i*i;
       priority_queue<NODE> k;
       for(int i=1;i<=10;i++)
       {
           NODE c;
           c.x=1;
           c.y=b[i];
           k.push(c);
           cout<<c.x<<"  "<<c.y<<endl;
       }
       cout<<k.size()<<endl;
       while(!k.empty())
       {
          NODE c=k.top(); cout<<c.x<<"  "<<c.y<<endl;
           k.pop();
       }
    
    }
  • 相关阅读:
    linux 首次登陆与线上求助
    003生信人必练
    计算器概论
    01 git 概念
    01 基因组学基本感念
    Python 函数习题
    Python字符编码详解,str,bytes
    python class
    [Leetcode]287. Find the Duplicate Number
    深度解析Word2vec
  • 原文地址:https://www.cnblogs.com/Andromeda-Galaxy/p/9509599.html
Copyright © 2011-2022 走看看