zoukankan      html  css  js  c++  java
  • 优先级队列

      1 #include <iostream>
      2 #include <queue>
      3 
      4 #include <functional>
      5 using namespace std;
      6 
      7 int main()
      8 {
      9     /*在优先队列中,优先级高的元素先出队列。
     10 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
     11 优先队列的第一种用法,也是最常用的用法:
     12 
     13 priority_queue<int> qi;
     14 通过<操作符可知在整数中元素大的优先级高。
     15 
     16     const int len=12;
     17     int a[len]={14,10,56,7,83,22,36,91,3,47,72,0};
     18     priority_queue<int> q1;
     19     for(int i=0;i<len;i++){
     20         q1.push(a[i]);
     21     }
     22     while(!q1.empty()){
     23         cout<<q1.top()<<"  ";
     24         q1.pop();
     25     }
     26 
     27     */
     28 
     29 
     30 
     31 /*
     32 **********************第二种方法:
     33 在示例1中,如果我们要把元素从小到大输出怎么办呢?
     34 这时我们可以传入一个比较函数,使用functional函数对象作为比较函数。
     35 
     36 priority_queue<int, vector<int>, greater<int> >q1;
     37 其中
     38 第二个参数为容器类型。
     39 第二个参数为比较函数。
     40 */
     41 
     42 
     43     const int len=12;
     44     int a[len]={14,10,56,7,83,22,36,91,3,47,72,0};
     45     priority_queue<int,vector<int>,less<int> > q1;
     46     for(int i=0;i<len;i++){
     47         q1.push(a[i]);
     48     }
     49     while(!q1.empty()){
     50         cout<<q1.top()<<"  ";
     51         q1.pop();
     52     }
     53 
     54 
     55 /*
     56 第三种方法:
     57 自定义优先级。
     58 
     59 struct node
     60 {
     61     friend bool operator< (node n1, node n2)
     62     {
     63         return n1.priority < n2.priority;
     64     }
     65     int priority;
     66     int value;
     67 };
     68 在该结构中,value为值,priority为优先级。
     69 通过自定义operator<操作符来比较元素中的优先级。
     70 在示例3中输出结果为:
     71 优先级  值
     72 9          5
     73 8          2
     74 6          1
     75 2          3
     76 1          4
     77 但如果结构定义如下:
     78 
     79 struct node
     80 {
     81     friend bool operator> (node n1, node n2)
     82     {
     83         return n1.priority > n2.priority;
     84     }
     85     int priority;
     86     int value;
     87 };
     88 则会编译不过(G++编译器)
     89 因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
     90 而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。
     91 
     92 */
     93 
     94 /*
     95 kuangbin写法:
     96 struct Node
     97 {
     98     int D,P;
     99 }node[MAXN];
    100 bool cmp1(Node a,Node b)
    101 {
    102     return a.D<b.D;
    103 }
    104 
    105 struct cmp
    106 {
    107     bool operator ()(int x, int y)
    108     {
    109         return x > y;// x小的优先级高
    110       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
    111     }
    112 };
    113 priority_queue<int, vector<int>, cmp>q;//定义方法
    114 
    115 
    116 strunct cmp{
    117 
    118     bool operator () (int x,int y)
    119     {
    120         return x > y;
    121     }
    122 }
    123 
    124 
    125 */
    126 
    127 
    128 
    129 
    130 
    131 
    132 
    133     return 0;
    134 }
  • 相关阅读:
    html基础教程2
    HTML的基础教程
    JS中响应函数和for循环执行问题
    Cookie显示上次访问时间出现错误的问题
    完全卸载VSCode--解决卸载重新安装后还有原来配置的问题
    servlet中输出HTML标签浏览器不能正常显示
    Java中Servlet输出中文乱码问题
    eclipse下配置Tomcat(没有Tomcat8及以上版本问题)
    Java多线程通信之wait()和notify()方法
    Java中synchronize关键字
  • 原文地址:https://www.cnblogs.com/vector11248/p/6255746.html
Copyright © 2011-2022 走看看