Why does the queue have entrance however the precedence queue has prime in stl?
The primary 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 have been added, whereas the weather in a precedence queue are processed based mostly on their precedence.
Queue:
A queue is a knowledge construction that follows the FIFO (First-In-First-Out) precept, which signifies that the primary component that’s added to the queue is the primary component that’s eliminated. The weather in a queue are processed within the order they have been added, and new parts are added to the again of the queue.
Within the STL, the queue container is carried out as an adapter on prime of different container lessons, corresponding to deque or checklist. The queue class offers a variety of member features, together with the “entrance” member operate, which returns a reference to the primary component within the queue. That is the component that shall be processed subsequent after we name the “pop” member operate to take away it from the queue.
For instance: let’s say now we have a queue of integers {1, 2, 3}. We will use the “entrance” member operate to entry the primary component within the queue, which is 1:
C++
|
If we then name the “pop” member operate to take away the primary component from the queue, the following component within the queue (which is 2) turns into the primary component, and we are able to entry it utilizing the “entrance” member operate once more:
C++
|
Precedence Queue
A precedence queue, then again, is a knowledge construction that orders parts based mostly on their precedence. The weather in a precedence queue are processed so as of their precedence, with the highest-priority component processed first. New parts are added to the precedence queue based mostly on their precedence order, and the highest-priority component is at all times on the entrance of the queue.
Within the STL, the precedence queue container is carried out as an adapter on prime 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 a variety of member features, together with the “prime” member operate, which returns a reference to the component with the best precedence within the queue. That is the component that shall be processed subsequent after we name the “pop” member operate to take away it from the queue.
For instance, let’s say now we have a precedence queue (Max-Heap) of integers {3, 1, 2}. We will use the “prime” member operate to entry the component with the best precedence, which is 3:
C++
|
If we then name the “pop” member operate to take away the highest-priority component from the queue, the following component with the best precedence (which is 2) turns into the brand new prime component, and we are able to entry it utilizing the “prime” member operate once more:
C++
|