队列stl queue<>_C ++ STL优先级队列– std :: priority_queue

队列stl queue<>

In this tutorial you will learn about STL priority queue in C++ i.e std::priority_queue and all functions applicable on it.

在本教程中,您将学习C ++中的STL优先级队列,即std :: priority_queue及其适用的所有功能。

std:: priority_queue is a container adaptor. This is almost same as queue container adaptor. i.e this also works as first in first out (FIFO). Elements always inserted at front position and deletion also done from front position. But only difference is elements in the priority queue has some priority. In this priority queue the element which is at top position has highest priority.

std :: priority_queue是容器适配器。 这几乎与队列容器适配器相同。 即,它也可以作为先进先出(FIFO)使用。 元素始终插入在最前面,删除也从最前面完成。 但是唯一的区别是优先级队列中的元素具有某些优先级。 在此优先级队列中,位于最高位置的元素具有最高优先级。

To use priority queue we simply include queue header file. There is no special priority queue header file. We include queue header file but to gain property of priority to the elements we declare as priority_queue. See below for more understanding.

要使用优先级队列,我们​​只需添加队列头文件即可。 没有特殊的优先级队列头文件。 我们包括队列头文件,但是为了获得我们声明为priority_queue的元素的优先级属性 请参阅下面的详细信息。

C ++ STL优先级队列– std :: priority_queue (C++ STL Priority Queue – std::priority_queue)

#include <queue> // this is enough to work with priority queue. But while declaring do

#include <queue> //这足以处理优先级队列。 但是在宣布做

priority_queue <data type> priority queue name;

priority_queue <数据类型>优先级队列名称

The functions associated with priority queue are:

与优先级队列关联的功能是:

push (element): To insert element into priority queue we use push operation.

push(element):要将元素插入优先级队列,我们​​使用push操作。

pop(): To remove element from priority queue we use pop operation.

pop():要从优先级队列中删除元素,我们使用弹出操作。

size(): To know the size of the priority queue we use size function. It returns number of elements that are present in priority queue.

size():要知道优先级队列的大小,我们使用size函数。 它返回优先级队列中存在的元素数。

top(): To get the first top element we use top function. It returns the most priority element in priority queue.

top():要获取第一个top元素,我们使用top函数。 它返回优先级队列中最优先的元素。

empty(): empty is a Boolean function which returns true if the priority queue is empty, otherwise returns false if priority queue is not empty.

empty(): empty是一个布尔函数,如果优先级队列为空,则返回true;否则,如果优先级队列不为空,则返回false。

swap():  If there are two priority queues with swap operation we can exchange all elements from priority queue1 to priority queue2 and vice versa. Here constraints are both must contain same data type of elements. But both sizes need not be equal.

swap():如果有两个具有交换操作的优先级队列,我们​​可以将所有元素从优先级队列1交换到优先级队列2,反之亦然。 这里的约束都必须包含相同的数据类型的元素。 但是两个大小不必相等。

Example program to show all above functions:

显示上述所有功能的示例程序:

#include <iostream>
#include <queue>
 
using namespace std;
 
void display (priority_queue <int> pq)
{
    priority_queue <int> prq = pq;
 
    while (!prq.empty())
    {
        cout << prq.top() << " ";
        prq.pop();
    }
    cout << endl;
}
 
int main ()
{
    priority_queue <int> prq;
 
    for (int i=0; i<5; i++){
		prq.push(i+1);
	}
 
    cout << "the elements in priority queue are " << endl;
	display (prq);
 
	cout << "size of the priority queue is ";
    cout << prq.size() << endl;
    cout << "top element in priority queue is " ;
	cout << prq.top() << endl;
 
    cout << "performing one pop operation..." << endl;
    prq.pop ();
	cout << "result after pop operation ";
    display (prq);
 
	// checking whether priority queue is empty or not
	if (prq.empty()){
		cout << "priority queue is empty" << endl;
	}
	else{
		cout << "priority queue is not empty " << endl;
	}
	
	// below code for swap operation
	priority_queue <int> prq2; //creating new priority queue
	for (int i=0; i<5; i++){
		prq2.push (i*10);
	}
 
	cout << endl << "Priority queue 1 elements before swapping are " << endl;
	display(prq);
 
	cout << endl << "Priority queue 2 elements before swapping are " << endl;
	display(prq2);
	prq.swap(prq2);
 
	cout << endl << "Priority queue 1 elements after swapping are " << endl;
	display(prq);
 
	cout << endl << "Priority queue 2 elements after swapping are " << endl;
	display(prq2);
 
    return 0;
}

Output

输出量

the elements in priority queue are 5 4 3 2 1 size of the priority queue is 5 top element in priority queue is 5 performing one pop operation… result after pop operation 4 3 2 1 priority queue is not empty

优先级队列中的元素是 5 4 3 2 1 优先级队列的大小是5优先级队列中的 顶部元素是5 执行一次弹出操作… 弹出操作后的结果4 3 2 1 优先级队列不为空

Priority queue 1 elements before swapping are 4 3 2 1

交换之前的优先级队列1元素是 4 3 2 1

Priority queue 2 elements before swapping are 40 30 20 10 0

交换之前的优先级队列2个元素是 40 30 20 10 0

Priority queue 1 elements after swapping are 40 30 20 10 0

交换后的优先级队列1元素为 40 30 20 10 0

Priority queue 2 elements after swapping are 4 3 2 1

交换后的优先级队列2个元素是 4 3 2 1

翻译自: https://www.thecrazyprogrammer.com/2017/10/stl-priority-queue.html

队列stl queue<>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值