Cpp Standard Library 简明教程

C++ Library - <priority_queue>

C++ Priority_queue 是 STL 中的一个容器适配器,它提供了动态优先队列数据结构。它以二进制堆的形式实现,允许高效地插入、删除和访问优先级最高或最低的元素。默认情况下,它会根据比较器函数按降序对元素进行排序。优先级队列常见于 Dijkstra 的最短路径和 Prim 的最小生成树等算法中。

The C++ Priority_queue is a container adaptor from the STL that provides the dynamic priority queue data structure. It is implemented as a binary heap, allowing efficient insertion, removal and access to the highest or lowest priority elements. By default, it orders elements in descending order based on a comparator function.Priority queues are commonly used in algorithms like Dijkstra’s shortest path and Prim’s minimum spanning tree.

Syntax

以下是 std::priority_queue 的语法。

Below is the syntax of std::priority_queue.

template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> < class priority_queue;

Parameters

  1. T − Type of the element contained. T may be substituted by any other data type including user-defined type.

  2. Container − Type of the underlying container object.

  3. Compare − Comparison object to be used to order the priority_queue. This may be a function pointer or function object that can compare its two arguments.

Member types

以下成员类型可作为参数或返回类型,由成员函数使用。

Following member types can be used as parameters or return type by member functions.

Functions from <priority_queue>

以下是来自 <priority_queue> 的所有方法的列表。

Below is list of all methods from <priority_queue> .

Constructors

Destructor

Member functions