C++ Queue and Priority Queue

Amongst the various Container Adaptors in C++, two of these are the Queue and Priority Queue. Container Adaptors are special containers that take other normal containers, and limit functionality in such a way that new functionality is born.


Queue

The Queue is an important C++ Container Adaptor like the stack, but with a fundamental difference. Queue’s follow the concept of F.I.F.O, also known as First In, First out. If 5 elements are added in the Queue, they will be taken out in the same order they were inserted (with the first element being the first, and last element being the last to be removed).

If you need help visualizing it, imagine a line of people (a.k.a as a queue). Those who entered the queue first, are the ones to leave it first. It follows a first come, first served concept.

Below is a short example demonstrating it’s use.

#include <iostream>
#include <queue>
using namespace std;


int main() {
    queue<int> q;

    q.push(4);
    q.push(1);
    q.push(3);

    while (!q.empty()) {
        cout << q.front() << endl;
        q.pop();
    }
}

As 4 was the first element entered into the Queue, it was also the first element to be accessed.

4
1
3

Queue Functions:

empty(): Returns whether the queue is empty.
size(): Returns the current size of the queue.
swap(): Swaps the contents of two Queue’s.
pop(): Removes the last element in the Queue.
push(elem): Pushes an element to the back of the Queue.
emplace(): Creates a new element, then pushes it to the back of the Queue.
front(): Returns a reference to the first element in the Queue.
back(): Returns a reference to the last element in the Queue.

You can learn more about Queue’s and how the above functions are used in our Tutorial on C++ Queues.


Priority Queue

The Priority Queue is a variant of the Queue Data Structure. It’s just like a regular Queue but has the concept of priority, which means those elements with the highest priority will be removed from the Queue first. As the highest priority element is removed first, we can say that priority queue’s have an degree of “sorting” in them.

The “Highest Priority” element is determined differently for different data types. For numerical values in the Queue, the highest number is picked. For strings and characters the one with the highest ASCII value is picked.

The below example demonstrates the difference between the Queue and Priority Queue.

#include <iostream>
#include <queue>
using namespace std;


int main() {
    priority_queue<int> qp;


    qp.push(4);
    qp.push(1);
    qp.push(3);

    while (!qp.empty()) {
        cout << qp.top() << endl;
        qp.pop();
    }
}

The largest values were accessed first, in contrast to the Queue.

4
3
1

Priority Queue Functions:

empty(): Returns whether the queue is empty.
size(): Returns the current size of the queue.
swap(): Swaps the contents of two Queue’s.
top(): Returns a reference to the top most element of the stack
pop(): Removes the last element in the Queue.
push(elem): Pushes an element to the back of the Queue.
emplace(): Creates a new element, then pushes it to the back of the Queue.


This marks the end of the C++ Queue and Priority Queue Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments