STL— priority_queue

 

一,定义

1,头文件

  <queue>

2,使用格式

  priority_queue<Type, Container, Functional>

    Type:数据类型

    Container:容器类型(默认用的是vector)

    Functional:比较的方式,默认是大顶堆,表现为降序队列。 

3,基操

  top: 访问队头元素

  push: 插入元素到队尾 (并排序)

  pop: 弹出队头元素

  empty: 队列是否为空

  size: 返回队列内元素个数

  swap: 交换内容 

  emplace: 原地构造一个元素并插入队列

4,例子

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<functional>
using namespace std;
int main(void)
{
	priority_queue <int, vector<int>, greater<int> > q;  // 升序队列
	//priority_queue <int, vector<int>, less<int> >q;     //降序队列

	q.push(4); q.push(2); q.push(3); q.push(1);
	while (!q.empty())
	{
		int vertex = q.top();
		q.pop();
		printf("%d ", vertex);
	}puts("");

	system("pause");
	return 0;
}

 

 

二,<  的重载,按照 dis 的大小升序排序

1,

typedef struct node    
{
    int id;    
    int dis;   
    node(){}
    node(int  a, int b) :id(a), dis(b) {}
    bool operator <(const node &a)const  // < 的重载
    {
        return a.dis < dis;
    }
}st;
priority_queue <st>q;

2,

typedef struct node   
{
    int id;
    int dis;
   node(){}
    node(int  a, int b) :id(a), dis(b) {}
    friend bool operator <(const node &a,const node &b)  // < 的重载
    {
        return a.dis > b.dis;
    }
}st;
priority_queue <st>q;

1,2 等效,为升序。

 

 

三,数组实现优先队列

思想:

  每次出队列后和入队列后都要维护好堆。这样就能一直保证堆顶元素是我们需要的最大值最小值或最大值。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int any;
typedef struct PriorityQueue // 小根堆
{
#define MaxSize 666
    any a[MaxSize];  // 数组模拟队列
    any b[MaxSize];  // 用数组 b 进行构造堆
    int first, rear; // 队首指针的前一位和队尾指针
    PriorityQueue() {      // 初始化队列
        first = rear = -1;
    }

    void swap(int i, int j)
    {
        int t = b[i];
        b[i] = b[j];
        b[j] = t;
    }
    void heapify(int n, int i)
    {
        if (i >= n)
            return;
        int c1 = 2 * i + 1;
        int c2 = 2 * i + 2;
        int min = i;
        if (c1 < n&&b[c1] < b[min])
            min = c1;
        if (c2 < n&&b[c2] < b[min])
            min = c2;
        if (min != i)
        {
            swap(min, i);
            heapify(n, min);
        }
    }
    void bulidHeap()
    {
        int n = rear - first;
        for (int i = 0; i < n; i++)
            b[i] = a[first + 1 + i];

        int last = n - 1;
        int p = last - 1 >> 1;
        for (int i = p; i >= 0; i--)
            heapify(n, i);

        for (int i = 0; i < n; i++)
            a[first + 1 + i] = b[i];
    }

    // 在队尾插入元素,需要重新建堆
    void push(any e) {
        a[++rear] = e;
        bulidHeap();
    }
    // 队首元素出队列,需要用 heapify 维护
    void pop() {
        first++;
        bulidHeap();
    }
    any top() {
        return a[first + 1];
    }
    bool empty() {
        return first == rear;
    }
    int size() {
        return rear - first;
    }
}pq;
int main(void)
{
    pq q;
    q.push(4);q.push(2);q.push(3);q.push(1);
    while (!q.empty())
    {
        int vertex = q.top();
        q.pop();
        printf("%d ", vertex);
    }puts("");

    system("pause");
    return 0;
}
View Code

 

 

 

========== ========= ======== ======= ====== ====== ==== === == =

连自己喜欢什么都不敢尝试的话,终究连自己都看不起自己 

 

posted @ 2020-03-30 00:25  叫我妖道  阅读(147)  评论(0编辑  收藏  举报
~~加载中~~