Software Development

Why does Queue have entrance however Precedence-queue has high in stl?

Why does Queue have entrance however Precedence-queue has high in stl?
Written by admin


Why does the queue have entrance however the precedence queue has high in stl?

The principle distinction between a queue and a precedence queue is {that a} queue follows the FIFO (First-In-First-Out) precept, whereas a precedence queue follows a particular precedence order. In different phrases, the weather in a queue are processed within the order they had been added, whereas the weather in a precedence queue are processed primarily based on their precedence.

Queue:

A queue is a knowledge construction that follows the FIFO (First-In-First-Out) precept, which signifies that the primary ingredient that’s added to the queue is the primary ingredient that’s eliminated. The weather in a queue are processed within the order they had been added, and new parts are added to the again of the queue.

Within the STL, the queue container is applied as an adapter on high of different container courses, resembling deque or record. The queue class offers plenty of member capabilities, together with the “entrance” member operate, which returns a reference to the primary ingredient within the queue. That is the ingredient that shall be processed subsequent after we name the “pop” member operate to take away it from the queue.

For instance: let’s say we’ve got a queue of integers {1, 2, 3}. We will use the “entrance” member operate to entry the primary ingredient within the queue, which is 1:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int important()

{

    queue<int> myQueue;

  

    myQueue.push(1);

    myQueue.push(2);

    myQueue.push(3);

  

    

    int firstElement = myQueue.entrance();

    cout << firstElement << endl;

    return 0;

}

If we then name the “pop” member operate to take away the primary ingredient from the queue, the subsequent ingredient within the queue (which is 2) turns into the primary ingredient, and we will entry it utilizing the “entrance” member operate once more:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int important()

{

    queue<int> myQueue;

  

    myQueue.push(1);

    myQueue.push(2);

    myQueue.push(3);

  

    myQueue.pop();

    int newFirstElement = myQueue.entrance();

  

    cout << newFirstElement << endl;

    return 0;

}

Precedence Queue

A precedence queue, then again, is a knowledge construction that orders parts primarily based on their precedence. The weather in a precedence queue are processed so as of their precedence, with the highest-priority ingredient processed first. New parts are added to the precedence queue primarily based on their precedence order, and the highest-priority ingredient is at all times on the entrance of the queue.

Within the STL, the precedence queue container is applied as an adapter on high of a vector or a deque, and it requires a comparability operate to find out the precedence order of parts. The precedence queue class offers plenty of member capabilities, together with the “high” member operate, which returns a reference to the ingredient with the very best precedence within the queue. That is the ingredient that shall be processed subsequent after we name the “pop” member operate to take away it from the queue.

For instance, let’s say we’ve got a precedence queue (Max-Heap) of integers {3, 1, 2}. We will use the “high” member operate to entry the ingredient with the very best precedence, which is 3:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int important()

{

    priority_queue<int> myPriorityQueue;

  

    myPriorityQueue.push(3);

    myPriorityQueue.push(1);

    myPriorityQueue.push(2);

  

    int highestPriorityElement

        = myPriorityQueue.high();

    cout << highestPriorityElement << endl;

    return 0;

}

If we then name the “pop” member operate to take away the highest-priority ingredient from the queue, the subsequent ingredient with the very best precedence (which is 2) turns into the brand new high ingredient, and we will entry it utilizing the “high” member operate once more:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int important()

{

    priority_queue<int> myPriorityQueue;

  

    myPriorityQueue.push(3);

    myPriorityQueue.push(1);

    myPriorityQueue.push(2);

  

    myPriorityQueue.pop();

    int newHighestPriorityElement = myPriorityQueue.high();

    cout << newHighestPriorityElement << endl;

    return 0;

}

About the author

admin

Leave a Comment