zoukankan      html  css  js  c++  java
  • 优先队列priority_queue的简单应用

    优先队列

    引入

    优先队列是一种特殊以及强大的队列。

    那么优先队列是什么呢? 
    说白了,就是一种功能强大的队列。

    它的功能强大在哪里呢? 
    四个字:自动排序。

    优先队列的头文件&&声明

    头文件:

    #include<queue>
    using namespace std;

    其次,一个优先队列声明的基本格式是: 
    priority_queue<结构类型> 队列名; 

    priority_queue<int> i;
    priority_queue<double> i;

    不过这是新手级别的,下面的是大佬级别的:

    priority_queue <node> q;
    //node是一个结构体
    //结构体里重载了‘<’小于符号
    priority_queue <int,vector<int>,greater<int> > q;
    //不需要#include<vector>头文件
    //注意后面两个“>”不要写在一起,“>>”是右移运算符
    priority_queue <int,vector<int>,less<int> >q;

    优先队列的基本操作

    与队列差不多:

    q.size();//返回q里元素个数
    q.empty();//返回q是否为空,空则返回1,否则返回0
    q.push(k);//在q的末尾插入k
    q.pop();//删掉q的第一个元素
    q.top();//返回q的第一个元素
    q.back();//返回q的末尾元素

    优先队列的特性

    默认的优先队列(非结构体结构)

    priority_queue <int> q;

    #include<cstdio>
    #include<queue>
    using namespace std;
    priority_queue <int> q;
    int main()
    {
        q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);
        while(!q.empty())
            printf("%d ",q.top()),q.pop();
    }

    程序大意就是在这个优先队列里依次插入10、8、12、14、6,再输出。 
    结果是什么呢? 
    14 12 10 8 6 
    也就是说,它是按从大到小排序的!

    默认的优先队列(结构体,重载小于)

    struct node
    {
        int x,y;
        bool operator < (const node & a) const
        {
            return x<a.x;
        }
    };

    这个node结构体有两个成员,x和y,它的小于规则是x小者小。 
    再来看看验证程序:

    #include<cstdio>
    #include<queue>
    using namespace std;
    struct node
    {
        int x,y;
        bool operator < (const node & a) const
        {
            return x<a.x;
        }
    }k;
    priority_queue <node> q;
    int main()
    {
        k.x=10,k.y=100; q.push(k);
        k.x=12,k.y=60; q.push(k);
        k.x=14,k.y=40; q.push(k);
        k.x=6,k.y=80; q.push(k);
        k.x=8,k.y=20; q.push(k);
        while(!q.empty())
        {
            node m=q.top(); q.pop();
            printf("(%d,%d) ",m.x,m.y);
        }
    }

    程序大意就是插入(10,100),(12,60),(14,40),(6,20),(8,20)这五个node。 
    再来看看它的输出: 
    (14,40) (12,60) (10,100) (8,20) (6,80)

    它也是按照重载后的小于规则,从大到小排序的。

    less和greater优先队列

    priority_queue <int,vector<int>,less<int> > p;
    priority_queue <int,vector<int>,greater<int> > q;

    less是从大到小,greater是从小到大。

    同时还可以自己定义规则 cmp;

    2、自定义优先级:

    struct cmp {     
      operator bool ()(int x, int y)     
      {        
         return x > y;   // x小的优先级高       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
      }
    };
    priority_queue<int, vector<int>, cmp> q;    //定义方法
    //其中,第二个参数为容器类型。第三个参数为比较函数。

    3、结构体声明方式:

    struct node {     
      int x, y;     
      friend bool operator < (node a, node b)     
      {         
        return a.x > b.x;    //结构体中,x小的优先级高     
      }
    };
    priority_queue<node>q;   //定义方法
    //在该结构中,y为值, x为优先级。
    //通过自定义operator<操作符来比较元素中的优先级。
    //在重载”<”时,最好不要重载”>”,可能会发生编译错误
    /*优先队列的基本使用    2017/8/1    xzxl*/ 
    #include<stdio.h> 
    #include<functional> 
    #include<queue> 
    #include<vector> 
    using namespace std; 
    //定义结构,使用运算符重载,自定义优先级1 
    struct cmp1{ 
        bool operator ()(int &a,int &b){ 
            return a>b;//最小值优先 
        } 
    }; 
    struct cmp2{ 
        bool operator ()(int &a,int &b){ 
            return a<b;//最大值优先 
        } 
    }; 
    //定义结构,使用运算符重载,自定义优先级2 
    struct number1{ 
        int x; 
        bool operator < (const number1 &a) const { 
            return x>a.x;//最小值优先 
        } 
    }; 
    struct number2{ 
        int x; 
        bool operator < (const number2 &a) const { 
            return x<a.x;//最大值优先 
        } 
    }; 
    int a[]={14,10,56,7,83,22,36,91,3,47,72,0}; 
    number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0}; 
    number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0}; 
       
    int main() 
    {   priority_queue<int>que;//采用默认优先级构造队列 
       
        priority_queue<int,vector<int>,cmp1>que1;//最小值优先 
        priority_queue<int,vector<int>,cmp2>que2;//最大值优先 
       
        priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误, 
                                                          //这是右移运算符,所以这里用空格号隔开 
        priority_queue<int,vector<int>,less<int> >que4;////最大值优先 
       
        priority_queue<number1>que5; 
        priority_queue<number2>que6; 
       
        int i; 
        for(i=0;a[i];i++){ 
            que.push(a[i]); 
            que1.push(a[i]); 
            que2.push(a[i]); 
            que3.push(a[i]); 
            que4.push(a[i]); 
        } 
        for(i=0;num1[i].x;i++) 
            que5.push(num1[i]); 
        for(i=0;num2[i].x;i++) 
            que6.push(num2[i]); 
       
       
        printf("采用默认优先关系:
    (priority_queue<int>que;)
    "); 
        printf("Queue 0:
    "); 
        while(!que.empty()){ 
            printf("%3d",que.top()); 
            que.pop(); 
        } 
        puts(""); 
        puts(""); 
       
        printf("采用结构体自定义优先级方式一:
    (priority_queue<int,vector<int>,cmp>que;)
    "); 
        printf("Queue 1:
    "); 
        while(!que1.empty()){ 
            printf("%3d",que1.top()); 
            que1.pop(); 
        } 
        puts(""); 
        printf("Queue 2:
    "); 
        while(!que2.empty()){ 
            printf("%3d",que2.top()); 
            que2.pop(); 
        } 
        puts(""); 
        puts(""); 
        printf("采用头文件"functional"内定义优先级:
    (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
    "); 
        printf("Queue 3:
    "); 
        while(!que3.empty()){ 
            printf("%3d",que3.top()); 
            que3.pop(); 
        } 
        puts(""); 
        printf("Queue 4:
    "); 
        while(!que4.empty()){ 
            printf("%3d",que4.top()); 
            que4.pop(); 
        } 
        puts(""); 
        puts(""); 
        printf("采用结构体自定义优先级方式二:
    (priority_queue<number>que)
    "); 
        printf("Queue 5:
    "); 
        while(!que5.empty()){ 
            printf("%3d",que5.top()); 
            que5.pop(); 
        } 
        puts(""); 
        printf("Queue 6:
    "); 
        while(!que6.empty()){ 
            printf("%3d",que6.top()); 
            que6.pop(); 
        } 
        puts(""); 
        return 0; 
    } 
    /*
    运行结果 :
    采用默认优先关系:
    (priority_queue<int>que;)
    Queue 0:
    83 72 56 47 36 22 14 10  7  3
      
    采用结构体自定义优先级方式一:
    (priority_queue<int,vector<int>,cmp>que;)
    Queue 1:
     7 10 14 22 36 47 56 72 83 91
    Queue 2:
    83 72 56 47 36 22 14 10  7  3
      
    采用头文件"functional"内定义优先级:
    (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
    Queue 3:
     7 10 14 22 36 47 56 72 83 91
    Queue 4:
    83 72 56 47 36 22 14 10  7  3
      
    采用结构体自定义优先级方式二:
    (priority_queue<number>que)
    Queue 5:
     7 10 14 22 36 47 56 72 83 91
    Queue 6:
    83 72 56 47 36 22 14 10  7  3
    */ 
  • 相关阅读:
    【Element UI】el-tooltip组件(提示消息) 换行
    复合文件CFB的存储结构及格式解析
    luogu P3801 红色的幻想乡 |容斥+树状数组
    luogu P3602 Koishi Loves Segments |堆+离散化贪心
    luogu P2048 [NOI2010] 超级钢琴 |堆+RMQ
    钉钉机器人使用注意事项
    k8s-部署
    docker 总结
    Navicat15最新版本破解 亲测可用!!!(Navicat Premium 注册出现 No All Pattern Found! File Already Patched)
    继续开始web的学习
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/8973325.html
Copyright © 2011-2022 走看看