Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Queue in C++ Standard

Template Library (STL)


Queues are a type of container adaptors that oper-
ate in a first in first out (FIFO) type of arrangement.
Elements are inserted at the back (end) and are
deleted from the front. Queues use an encapsu-
lated object of deque or list (sequential container
class) as its underlying container, providing a spe-
cific set of member functions to access its ele-
ments. Following is an example to demonstrate the
queue and its various methods.
// CPP code to illustrate Queue in
// Standard Template Library (STL)
#include <iostream>
#include <queue>

using namespace std;

// Print the queue


void showq(queue<int> gq)
{
queue<int> g = gq;
while (!g.empty()) {
cout << '\t' << g.front();
g.pop();
}
cout << '\n';
}

// Driver Code
int main()
{
queue<int> gquiz;
gquiz.push(10);
gquiz.push(20);
gquiz.push(30);

cout << "The queue gquiz is : ";


showq(gquiz);

cout << "\ngquiz.size() : " << gquiz.size();


cout << "\ngquiz.front() : " << gquiz.front();
cout << "\ngquiz.back() : " << gquiz.back();

cout << "\ngquiz.pop() : ";


gquiz.pop();
showq(gquiz);

return 0;
}

Output
The queue gquiz is : 10 20 30
gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() : 20 30
Methods of Queue are:

Method Definition

queue::em Returns whether the queue is empty.


pty()

queue::siz Returns the size of the queue.


e()

queue::sw Exchange the contents of two


ap() queues but the queues must be of the
same type, although sizes may differ.

queue::em Insert a new element into the queue


place() container, the new element is added
to the end of the queue.

queue::fro Returns a reference to the first


nt() element of the queue.

queue::bac Returns a reference to the last


k() element of the queue.

queue::pus Adds the element ‘g’ at the end of the


h(g) queue.
queue::pop Deletes the first element of the
() queue.

Priority Queue in C++ Stan-


dard Template Library
(STL)
Priority queues are a type of container adapters,
specifically designed such that the first element of
the queue is the greatest of all elements in the
queue and elements are in nonincreasing order
(hence we can see that each element of the queue
has a priority {fixed order}). Following is an exam-
ple to demonstrate the priority queue and its vari-
ous methods.
// CPP Program to demonstrate Priority Queue
#include <iostream>
#include <queue>
using namespace std;

void showpq(priority_queue<int> gq)


{
priority_queue<int> g = gq;
while (!g.empty()) {
cout << '\t' << g.top();
g.pop();
}
cout << '\n';
}

// Driver Code
int main()
{
priority_queue<int> gquiz;
gquiz.push(10);
gquiz.push(30);
gquiz.push(20);
gquiz.push(5);
gquiz.push(1);

cout << "The priority queue gquiz is : ";


showpq(gquiz);

cout << "\ngquiz.size() : " << gquiz.size();


cout << "\ngquiz.top() : " << gquiz.top();

cout << "\ngquiz.pop() : ";


gquiz.pop();
showpq(gquiz);

return 0;
}

Output
The priority queue gquiz is : 30 20 10 5 1

gquiz.size() : 5
gquiz.top() : 30
gquiz.pop() : 20 10 5 1
Note: By default, C++ creates a max-heap for prior-
ity queue.
How to create a min-heap for the priority queue?
C++ provides the below syntax for the same.
Syntax:

priority_queue <int, vector<int>, greater<int>> g = gq;


• CPP
// C++ program to demonstrate min heap for priority queue
#include <iostream>
#include <queue>
using namespace std;

void showpq(
priority_queue<int, vector<int>, greater<int> > gq)
{
priority_queue<int, vector<int>, greater<int> > g =
gq;
while (!g.empty()) {
cout << '\t' << g.top();
g.pop();
}
cout << '\n';
}

// Driver Code
int main()
{
priority_queue<int, vector<int>, greater<int> >
gquiz;
gquiz.push(10);
gquiz.push(30);
gquiz.push(20);
gquiz.push(5);
gquiz.push(1);

cout << "The priority queue gquiz is : ";


showpq(gquiz);

cout << "\ngquiz.size() : " << gquiz.size();


cout << "\ngquiz.top() : " << gquiz.top();

cout << "\ngquiz.pop() : ";


gquiz.pop();
showpq(gquiz);

return 0;
}
Output
The priority queue gquiz is : 1 5 10 20 30

gquiz.size() : 5
gquiz.top() : 1
gquiz.pop() : 5 10 20 30
Note: The above syntax may be difficult to remem-
ber, so in case of numeric values, we can multiply
the values with -1 and use max heap to get the ef-
fect of min heap.
Methods of Priority Queue are:

Method Definition

priority_queue:: Returns whether the queue is


empty() empty.

priority_queue:: Returns the size of the queue.


size()

priority_queue:: Returns a reference to the


top() topmost element of the queue.

priority_queue:: Adds the element ‘g’ at the end


push() of the queue.

priority_queue:: Deletes the first element of the


pop() queue.

priority_queue:: Used to swap the contents of


swap() two queues provided the queues
must be of the same type,
although sizes may differ.

priority_queue:: Used to insert a new element


emplace() into the priority queue
container.

priority_queue Represents the type of object


value_type stored as an element in a
priority_queue. It acts as a
synonym for the template
parameter.

You might also like