Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ container class

The C++ container class

In C++, is part of the Standard Template Library (STL), used to implement a priority queue.

A priority queue is a special kind of queue that allows us to quickly access the element with the highest (or lowest) priority in the queue.

In C++,priority_queue By default it is a max-heap, which means the top element of the queue always has the largest value.

priority_queue is a container adapter that provides heap operations on the underlying container. It does not provide iterators and does not support random access.

Syntax

The following are priority_queue 's basic syntax:

#include 

// Declare an integer priority queue
priority_queue pq;

// Declare a custom priority queue, requires a comparison function
struct compare {
    bool operator()(int a, int b) {
        return a > b; // 这里定义了最小堆
    }
};
priority_queue, compare> pq_min;

common operations

Example

Below is an example using priority_queue simple example, we will create a max-heap and show how to add elements and get the top element of the queue.

Example

#include

#include

int

main

(

)

{

   

// Create an integer priority queue

    std

::

priority_queue

<
int
pq

;

   

// Add elements to the priority queue

    pq.

push

(

30

)

;

    pq.

push

(

10

)

;

    pq.

push

(

50

)

;

    pq.

push

(

20

)

;

   

// Output the elements in the queue

    std

::

cout

<<

"Elements in the queue:"

<<

std

::

endl

;

   

while

(

!

pq.

empty

(

)

)

{

        std

::

cout

<<

pq.

top

(

)

<<

std

::

endl

;

        pq.

pop

(

)

;

   

}

   

return

0

;

}

Output result:

队列中的元素:
50
30
20
10

Custom priority

If you need a min-heap, you can implement it with a custom comparison function:

Example

#include

#include

#include

struct

compare

{

   

bool

operator

(

)

(

int

a,

int

b

)

{

       

return

a

b

;

// Define a min-heap

   

}

}

;

int

main

(

)

{

   

// Create a priority queue of a custom type, using a min-heap

    std

::

priority_queue

<

int

, std

::

vector

<

int

, compare

pq_min

;

   

// Add elements to the priority queue

    pq_min.

push

(

30

)

;

    pq_min.

push

(

10

)

;

    pq_min.

push

(

50

)

;

    pq_min.

push

(

20

)

;

   

// Output the elements in the queue

    std

::

cout

<<

"Elements in the min-heap:"

<<

std

::

endl

;

   

while

(

!

pq_min.

empty

(

)

)

{

        std

::

cout

<<

pq_min.

top

(

)

<<

std

::

endl

;

        pq_min.

pop

(

)

;

   

}

   

return

0

;

}

Output result:

最小堆中的元素:
10
20
30
50

is a very useful container in the C++ STL, especially suited for scenarios requiring fast access to the highest or lowest priority element. By customizing the comparison function, we can easily implement a max-heap or min-heap. I hope this article helps beginners better understand and use priority_queue.