c语言中优先级队列
A Priority Queue is a variant of a Queue such that it’s elements are ordered based on their priority. C++ has a built-in priority queue data structure in it’s Standard Template Library (STL).
优先级队列是队列的一种变体,可以根据其优先级对元素进行排序。 C ++在其标准模板库( STL )中具有内置的优先级队列数据结构。
Priority queues are implemented as container adapters in C++.
优先级队列在C ++中作为容器适配器实现。
This means that like other container adapters, they have member functions for the container objects.
这意味着像其他容器适配器一样,它们具有容器对象的成员功能。
Let’s understand how we can use the Priority Queue container in C++ to make our own priority queues.
让我们了解如何使用C ++中的Priority Queue容器创建自己的优先级队列。
创建一个优先队列 (Create a Priority Queue)
We can create a priority queue by declaring a priority queue variable of the type std::priority_queue<type>
.
我们可以通过声明类型为std::priority_queue<type>
的优先级队列变量来创建优先级队列。
The std::
namespace signifies that this supports STL (Standard Template Library) operations.
std::
名称空间表示它支持STL (标准模板库)操作。
NOTE: To use this, we must include the <queue>
header file in our program.
注意 :要使用此功能,我们必须在程序中包含<queue>
头文件。
Since this is a container of the STL, we must provide the template type for the queue. It could be anything from int
, float
, char
, string
, etc.
由于这是STL的容器,因此我们必须提供队列的模板类型。 它可以是int
, float
, char
, string
等任何东西。
For example, the follow