Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Priority queue in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 480
    Comment on it

    Priority queue is container adapter, designed in such a way that the first element is the greatest elements among it contains.
    In priority queue elements are popped from the back which act as top in the priority queues.
    In the below program we are going to insert 10 random integer type values in the priority queue, then we will print the top value of the priority queue which will be the maximum of all the values contained in the priority queue, after that we perform pop operation. We will continue this process until the priority queue is empty.

    Sample program using priority queue

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    int main()
    {
            priority_queue <int>  pq;
            int tmp;
            int i = 10;
            while(i--) {
                    cin >> tmp;
                    pq.push(tmp); // pushing the value in priority queue
            }
    
            while (!pq.empty()) {
                    cout << pq.top() << " "; //print the top of priority queue
                    pq.pop(); //pop the priority queue
            }
    
            cout << endl;
    
            return 0;
    }
    

    Sample input for the above program :

    7 234 3 45 63 42 4 34 6 67
    

    Output of the program using the above input :

    234 67 63 45 42 34 7 6 4 3
    

    Member functions

    Element access
    top

    Capacity
    empty
    size

    Modifiers
    push
    emplace
    pop
    swap

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: