优先队列入门

C++ Priority_Queue(优先队列)

优先队列是一种具有优先级的队列,对常规的队列取值是基于先进先出的顺序,而在优先队列中,选择具有了优先性。
队列:
1.back() 返回一个引用,指向最后一个元素
2.empty() 如果队列空则返回真
3.front() 返回第一个元素
4.pop() 删除第一个元素
5.push() 在末尾加入一个元素
6.size() 返回队列中元素的个数
优先队列:
1.empty() 如果优先队列为空,则返回真
2.pop() 删除第一个元素
3.push() 加入一个元素
4.size() 返回优先队列中拥有的元素的个数
5.top() 返回优先队列中有最高优先级的元素


示例代码如下:

<span style="font-size:18px;">#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;

int main()
{
    queue<int> que;//队列

    int x[9] = {1,2,1,2,1,2,3,1,3};
    for(int i = 0;i < 9;i++){
        que.push(x[i]);
    }
    for(int i = 0;i < 9;i++){
        printf("%d ",que.front());//结果:1 2 1 2 1 2 3 1 3
        que.pop();
    }
    printf("\n");
    return 0;
}</span>

<span style="font-size:18px;">#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;

int main()
{
    //priority_queue<int,vector<int>,greater<int> > que;// 从小到大 注意空格用于区分位运算
    //priority_queue<int,vector<int>,less<int> > que;// 从大到小
    priority_queue<int > que; // 默认从大到小

    int x[9] = {1,1,1,2,2,2,3,3,3};
    for(int i = 0;i < 9;i++){
        que.push(x[i]);
    }
    for(int i = 0;i < 9;i++){
        printf("%d ",que.top());//结果:3 3 3 2 2 2 1 1 1
        que.pop();
    }
    printf("\n");
    return 0;
}</span>
对于普通的数据类型的大小排列我们可以直接通过如上的方法定义优先级,但是在我们使用结构体或者需要更为复杂的优先级判断时,就我们需要自己对优先级进行设定。

<span style="font-size:18px;">#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;

struct Dian{
    int a;
    int b;
    friend bool operator > (Dian x,Dian y){//  ">"代表对从小到大的优先级判断重载
       if(x.a != y.a)
         return x.a > y.a;
       else
         return x.b < y.b;
    }
}dian;

int main()
{
    priority_queue<Dian,vector<Dian>,greater<Dian> > que;// 对应“>”
    //priority_queue<Dian,vector<Dian>,less<Dian> > que;// 对应“<”
    //priority_queue<Dian > que; // 默认从大到小
    int x[9] = {1,1,1,2,2,2,3,3,3};
    int y[9] = {1,2,3,1,2,3,1,2,3};
    for(int i = 0;i < 9;i++){
        dian.a = x[i];
        dian.b = y[i];
        que.push(dian);
    }
    printf("a b\n");
    for(int i = 0;i < 9;i++){
        printf("%d %d\n",que.top().a,que.top().b);
        que.pop();
    }
    printf("\n");
    return 0;
}
/*结果
a b
1 3
1 2
1 1
2 3
2 2
2 1
3 3
3 2
3 1
*/</span>

增添一种新的方法:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
    int x;
    bool operator < (const node &a)const
    {
        return a.x < x;//这种比较方法为按从小到大顺序,与a.x<b.x时相反
    }
}a;

int main()
{
    priority_queue<node> que;
    for(int i = 10;i > 0;i--){
        a.x = i;
        que.push(a);
    }
    for(int i = 0;i < 10;i++){
        node k = que.top();
        que.pop();
        printf("%d\n",k.x);
    }

    return 0;
}







### 队列和优先队列的概念 #### 队列概述 队列是一种遵循先进先出(FIFO, First In First Out)原则的线性数据结构。这意味着最早进入队列的元素会最先被移除。为了更高效地利用存储空间并解决顺序队列中的“假溢出现象”,通常采用循环队列的形式,即一种头尾相接的顺序存储结构[^2]。 #### 优先队列定义 不同于普通队列按照加入顺序处理元素的方式,优先队列根据预先设定的关键字来决定哪个元素应当首先被取出。这种关键字可以代表任务的重要性或其他形式的权重,在某些应用场景下非常有用。具体来说,当执行`DeleteMin`操作时,将会找到、返回并移除具有最低关键字值的那个成员;而通过`Insert`则可向其中添加新的项目[^4]。 ### 实现方式对比 #### 常规队列入门级实现 对于简单的队列而言,可以通过数组或链表来进行构建。然而,考虑到性能因素以及内存管理上的便利性,这里推荐使用固定大小的数组作为基础容器,并配合两个指针分别指向当前头部位置与尾部位置以追踪有效范围内的所有节点。每当有新条目进来的时候就更新这些索引变量的位置直到达到预设容量上限为止[^3]。 ```java // Java Example of a simple queue using an array with fixed capacity. public class SimpleQueue<T> { private T[] elements; private int head = 0; // Index for the front element private int tail = -1; // Index where next insertion will occur private final int CAPACITY; @SuppressWarnings("unchecked") public SimpleQueue(int cap){ this.CAPACITY = cap; this.elements = (T[]) new Object[CAPACITY]; } public boolean enqueue(T item){ /* ... */ } public T dequeue(){ /* ... */ } } ``` #### 优先队列高级特性支持下的实现方案 鉴于上述提到的功能需求,最常见也最为有效的做法就是基于二叉堆这一特定类型的完全二叉树模型之上建立起来。该种结构不仅能够很好地满足基本功能的要求,而且还能保证每次插入/删除操作的时间复杂度维持在一个较低水平上(O(log n))。因此非常适合用于那些需要频繁访问最大值或者最小值的应用场景之中[^1]。 ```c++ #include <vector> using namespace std; template<typename T> class PriorityQueue { private: vector<T> heap; protected: void upAdjust(); // Adjust upward after insertions void downAdjust(size_t pos); // Adjust downward during deletions public: bool isEmpty() const {return heap.empty();} void push(const T& value); T pop(); }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值