对优先队列的一点点理解!

     暑期集训正式拉开帷幕。不过,算法还是靠自己学滴!

     本来寒假集训的时候就学过这些,不过并不是很深入地了解,而且也没接触相关的题。今天花了一上午有找了找博客看看。本文是我对优先队列的一点点理解,以后还会继续整理编辑。

     我们一般是先接触队列这种FIFO(first in,first out)的顺序链式数据结构,不过在某些地方要优先选取队列中的每个元素,这时候就要用到优先对列了。优先队列,顾名思义,就是具有优先权高的元素先出队列,删除操作呢就是将优先权最高的删除;元素被赋予了优先级,分最大优先队列与最小优先队列两种。最大优先队列就是优先选取优先权最大的元素,最小优先对列同之。与普通队列一样,优先队列也有三种操作:①插入②查找③删除;其中查找与删除都是先考虑优先权最高或最小的元素;

      那么我们先来看看我自己写的一个帮助理解的简单优先列吧:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,i;
    while(~scanf("%d",&n))
    {
        queue<int>q;//普通对列;
        priority_queue<int>q1;//默认优先对列;
        priority_queue<int,vector<int>,greater<int> >q2;//头文件functional内定义优先对列;最小优先;
        priority_queue<int,vector<int>,less<int> >q3;//头文件functional内定义优先对列;最大优先;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a);
            q.push(a);
            q1.push(a);
            q2.push(a);
            q3.push(a);
        }
         for(i=0;i<n;i++)
        {
            printf("%d ",q.front());
            q.pop();
        }
        printf("//普通队列\n");
        for(i=0;i<n;i++)
        {
            printf("%d ",q1.top());
            q1.pop();
        }
        printf("//默认优先队列\n");
        for(i=0;i<n;i++)
        {
            printf("%d ",q2.top());
            q2.pop();
        }
       printf("//functional内定义的优先队列\n");
    }
    return 0;
}

  以上代码只介绍了用头文件functional内定义的优先对列,也可以自定义优先对列;

   一下是我从两篇博客看到的代码,介绍很详细,各有特色:

1.0

#include<queue>
#include<iostream>
#include<functional>
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;
    int a[len] = {3,5,9,6,2};

    /****示例1****/
    priority_queue<int> qi;
    for(i = 0; i < len; i++)
        qi.push(a[i]);//入队列;
    for(i = 0; i < len; i++)
    {
        cout<<qi.top()<<" ";//按默认优先级输出;
        qi.pop();
    }
    cout<<endl;

    /****示例2****/
    priority_queue<int, vector<int>, greater<int> >qi2//采用头文件\"functional\"内定义优先级;
    for(i = 0; i < len; i++)
        qi2.push(a[i]);
    for(i = 0; i < len; i++)
    {
        cout<<qi2.top()<<" ";
        qi2.pop();
    }
    cout<<endl;

    /****示例3****/
    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<<"优先级"<<'\t'<<"值"<<endl;
    for(i = 0; i < len; i++)
    {
        cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
        qn.pop();
    }
    return 0;
}//可以看看输出情况;


2.0
#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("采用默认优先关系:\n(priority_queue<int>que;)\n");//最大值优先;
    printf("Queue 0:\n");
    while(!que.empty())
    {
        printf("%3d",que.top());
        que.pop();
    }
    puts("\n");
    printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
    printf("Queue 1:\n");
    while(!que1.empty())
    {
        printf("%3d",que1.top());
        que1.pop();
    }
    puts("");
    printf("Queue 2:\n");
    while(!que2.empty())
    {
        printf("%3d",que2.top());
        que2.pop();
    }
    puts("\n");
    printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
    printf("Queue 3:\n");
    while(!que3.empty())
    {
        printf("%3d",que3.top());
        que3.pop();
    }
    puts("");
    printf("Queue 4:\n");
    while(!que4.empty())
    {
        printf("%3d",que4.top());
        que4.pop();
    }
    puts("\n");
    printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
    printf("Queue 5:\n");
    while(!que5.empty())
    {
        printf("%3d",que5.top());
        que5.pop();
    }
    puts("");
    printf("Queue 6:\n");
    while(!que6.empty())
    {
        printf("%3d",que6.top());
        que6.pop();
    }
    puts("");
    return 0;
}


博主水平有限,望各位大神见谅。

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值