c++——优先队列(priority_queue)

优先队列详解/C++
优先队列
1.概念:什么是优先队列呢?在优先队列中,元素被赋予优先级,当访问元素时,具有最高级优先级的元素先被访问 .即优先队列具有最高级先出的行为特征。它可以说是队列和排序的完美结合体,不仅可以存储数据,还可以将这些数据按照我们设定的规则进行排序。

2.定义:优先队列在头文件#include 中;其声明格式为:priority_queue ans;//声明一个名为ans的整形的优先队列

3.支持的操作:

q.empty()          //如果队列为空,则返回true,否则返回false
q.size()              //返回队列中元素的个数
q.pop()              //删除队首元素,但不返回其值
q.top()               //返回具有最高优先级的元素值,但不删除该元素
q.push(item)      //在基于优先级的适当位置插入新元素

4实例:

优先队列的时间复杂度为O(logn),n为队列中元素的个数,其存取都需要时间。

在默认的优先队列中,优先级最高的先出队默认的int类型的优先队列中先出队的为队列中较大的数

然而更多的情况下,我们是希望可以自定义其优先级的,下面介绍定义优先级的操作

priority_queue 对于基本类型的使用方法相对简单。他的模板声明带有三个参数:

priority_queue<Type, Container, Functional> 其中Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.STL里面默认用的是 vector.

1.比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,优先队列就是大顶堆,队头元素最大。(也就是默认非升序)
priority_queue q;

#include <iostream>
#include <queue> 
using namespace std; 
int main(){
    priority_queue<int> q;     
    for( int i= 0; i< 10; ++i ) q.push( rand() );
    while( !q.empty() ){
        cout << q.top() << endl;
        q.pop();
    }     
    getchar();
    return 0;
}

在这里插入图片描述
2.如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆(非降序)
priority_queue<int, vector, greater > q;

#include <iostream>
#include <queue> 
using namespace std; 
int main(){
    priority_queue<int, vector<int>, greater<int> > q;
    for( int i= 0; i< 10; ++i ) q.push( rand() );
    while( !q.empty() ){
        cout << q.top() << endl;
        q.pop();
    }     
    getchar();
    return 0;
}

在这里插入图片描述
3.对于自定义类型,则必须自己重载 operator<
priority_queue q;

#include <iostream>
#include <queue> 
using namespace std; 
struct Node{
    int x, y;
    Node( int a= 0, int b= 0 ):
        x(a), y(b) {}
};
 
bool operator<( Node a, Node b ){//升序 
    if( a.x== b.x ) return a.y> b.y;
    return a.x> b.x; 
}
 
int main(){
    priority_queue<Node> q;     
    for( int i= 0; i< 10; ++i )
    q.push( Node( rand(), rand() ) );  //构造函数   
    while( !q.empty() ){
        cout << q.top().x << ' ' << q.top().y << endl;
        q.pop();
    }     
    getchar();
    return 0;
}

在这里插入图片描述
4.自定义cmp
priority_queue<Node, vector, cmp> q;

#include <iostream>
#include <queue> //小顶堆
using namespace std; 
struct Node{
    int x, y;
    Node( int a= 0, int b= 0 ):
        x(a), y(b) {}
};
 
struct cmp{
    bool operator() ( Node a, Node b ){
        if( a.x== b.x ) return a.y> b.y;         
        return a.x> b.x; }
};
 
int main(){
    priority_queue<Node, vector<Node>, cmp> q;     
    for( int i= 0; i< 10; ++i )
    q.push( Node( rand(), rand() ) );
     
    while( !q.empty() ){
        cout << q.top().x << ' ' << q.top().y << endl;
        q.pop();
    }     
    getchar();
    return 0;
}  

在这里插入图片描述

  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是C语言优先队列的应用示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_HEAP_SIZE 100 typedef struct { int key; /* 其他数据字段 */ } Element; typedef struct { Element heap[MAX_HEAP_SIZE]; int size; } Heap; Heap *create_heap() { Heap *heap = (Heap *) malloc(sizeof(Heap)); heap->size = 0; return heap; } int parent(int i) { return (i - 1) / 2; } int left_child(int i) { return 2 * i + 1; } int right_child(int i) { return 2 * i + 2; } void swap(Element *a, Element *b) { Element temp = *a; *a = *b; *b = temp; } void insert(Heap *heap, Element e) { if (heap->size >= MAX_HEAP_SIZE) { printf("Error: heap overflow!\n"); exit(EXIT_FAILURE); } int i = heap->size++; heap->heap[i] = e; while (i > 0 && heap->heap[i].key > heap->heap[parent(i)].key) { swap(&heap->heap[i], &heap->heap[parent(i)]); i = parent(i); } } Element delete_max(Heap *heap) { if (heap->size <= 0) { printf("Error: heap underflow!\n"); exit(EXIT_FAILURE); } Element max = heap->heap[0]; heap->heap[0] = heap->heap[--heap->size]; int i = 0; while (left_child(i) < heap->size) { int j = left_child(i); if (right_child(i) < heap->size && heap->heap[right_child(i)].key > heap->heap[left_child(i)].key) { j = right_child(i); } if (heap->heap[i].key >= heap->heap[j].key) { break; } swap(&heap->heap[i], &heap->heap[j]); i = j; } return max; } int main() { Heap *heap = create_heap(); Element e1 = {10}; insert(heap, e1); Element e2 = {20}; insert(heap, e2); Element e3 = {30}; insert(heap, e3); Element e4 = {5}; insert(heap, e4); Element e5 = {15}; insert(heap, e5); Element e6 = {25}; insert(heap, e6); printf("Heap elements: "); while (heap->size > 0) { Element max = delete_max(heap); printf("%d ", max.key); } printf("\n"); return 0; } ``` 在这个示例中,我们定义了一个元素(Element)结构体,其中包含一个关键字(key)字段和其他数据字段。我们还定义了一个堆(Heap)结构体,其中包含一个存储元素数组和堆大小的字段。 我们实现了堆的基本操作,例如插入和删除最大元素。插入操作将元素插入堆中,并将其上移以满足堆的性质。删除操作将堆的根节点删除,并将其替换为堆的最后一个元素。然后,它将新根节点下移以满足堆的性质。 在main函数中,我们创建了一个堆,并插入了一些元素。然后,我们执行删除操作,直到堆为空,并打印删除的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值