C + + STL priority queue_ queue)

Time:2021-5-4

std::priority_queue

Priority queue

  1、firstThe element is alwaysmaximumElements.

2. It has heap like features, in which elements can be inserted at any time.

   3. SupportSubscript access(random access iterator)

The implementation inside the priority queue needs to rely onFoundation vesselThe container should be passablerandom access iterators Access and need to support the following operations

  • empty( )

  • size( )

  • front( )

  • push_back( )

  • pop_back( )

      it is obvious that there aredequeandvectorThese two basic containers support the above operations

     so by default, if notpriority_queueSpecifies the underlying container class, thevector

Member function

(constructor) Construct priority queue (public member function )
empty Is the priority queue empty
size Returns the current number of elements in the priority queue
top Access the top element (returns the constant reference to the top element)
push Insert an element
pop Remove top element
emplace Construct and insert an element
void swap (priority_queue& x) Exchange the contents of two queues

Note:
1. Compared with push, empty optimizes the use of memory space, which can be queried separately
2. Swap is the exchange of all elements in two priority queues of the same type, such asa.swap ( x )That is to exchange all elements of queue a and X

Construct priority queue

/* 1 */ priority_ queue pq1;                         // The default large root heap and the default base container is vector
/* 2 */ priority_ queue, less > pq2;     // As like as two peas in 1.
/* 3 */ priority_ queue, greater > pq3;   // Small root pile and deque as base container

be careful:Big root pilelessThe small root pile isgreater

Function member use case

1. Push, top, empty, pop, big root heap

(1)int
#include 
#include 

using namespace std;

int main ( void )
{
    priority_ queue pq; // Big root heap, descending by default (big first, small last)

    pq.push ( 60 );
    pq.push ( 20 );
    pq.push ( 40 );
    pq.push ( 1 );
    pq.push ( 25 );
    
    while ( ! PQ. Empty()) // if PQ is not empty, loop
    {
        cout << pq.top() << " "; // Add new elements
        pq.pop();    // Pop up header element
    }

    return 0;
}

(2)string
#include 
#include 

using namespace std;

int main ( void )
{
    priority_ queue pq; // Big root heap, descending by default (big first, small last)

    pq.push ( "abc" );
    pq.push ( "abd" );
    pq.push ( "acd" );
    pq.push ( "cda" );
    pq.push ( "abcd" );
    
    while ( ! PQ. Empty()) // if PQ is not empty, loop
    {
        cout << pq.top() << endl; // Add new elements
        pq.pop();    // Pop up header element
    }

    return 0;
}

Output in dictionary order

2. Swap, empty, small root heap

(1) Input and output
#include 
#include 

using namespace std;

int main ( void )
{
    priority_ queue, greater > pq1; // Small root heap, descending by default (small first, big last)

    pq1.emplace ( 5 );
    pq1.emplace ( 4 );
    pq1.emplace ( 3 );
    pq1.emplace ( 2 );
    pq1.emplace ( 1 );    

    priority_queue, greater > pq2;

    pq2.emplace ( 5 * 2 );
    pq2.emplace ( 4 * 2 );
    pq2.emplace ( 3 * 2 );
    pq2.emplace ( 2 * 2 );
    pq2.emplace ( 1 * 2 );

    cout << "pq1:" << endl;
    while ( ! Pq1. Empty()) // if PQ is not empty, loop
    {
        cout << pq1.top() << " "; // Add new elements
        pq1.pop();    // Pop up header element
    }
    
    cout << endl << "pq2:" << endl;
    while ( ! Pq2. Empty()) // if PQ is not empty, loop
    {
        cout << pq2.top() << " "; // Add new elements
        pq2.pop();    // Pop up header element
    }
    cout << endl;
    
    return 0;
}

(2) UtilizationswapEfficient queue clearing
void clear( priority_queue &pq ) {
    priority_queue empty;
    pq.swap ( empty );
}
(3) Utilization=Efficient queue clearing
void clear( priority_queue &pq ) {
    priority_queue t;
    pq = t;
}