1.
Introduction
2.
Implementation
2.1.
Program
3.
3.1.
What is a pair data type?
3.2.
What is the FIFO rule?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Queue of Pairs in C++ STL

Ishita Chawla
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Queue is an abstract data structure(linear), much similar to a stack. It follows the FIFO rule, First In First Out, where the elements are inserted from the last and removed from the front of the queue. It is the most basic Data Structure and is used to implement several other data structures like dequepriority queue, and double-ended priority queue. It is also used to implement Radix Sort and Breadth-First Search(BFS). It also has several applications in Operating Systems like Semaphores, First Come First Serve(FCFS), buffer, etc.

The queue of pairs is a container containing pairs as the elements of the queues. The first element is referred to as first and the second element is referred to as second and their relative order is fixed in the form of {first, second}.

It is used to implement complex data structures and is also used to solve several questions of competitive programming.

C++ also provides template classes to implement commonly used data structures and algorithms like stacks, queues, vectors, sorting, etc. It is referred to as the Standard Template Library(STL) and is a very powerful tool of C++. This blog will discuss the implementation of the queue of pairs in C++ using STL.

The working of queue of pairs is the same as that of a normal queue, and it has also been shown below for a better understanding.

The general STL template that is used to define a queue of pairs is:

You can also compile this code with the help of Online C++ Compiler

Implementation

Program

``````// C++ program to illustrate queue of pairs using STL.
#include <iostream>
#include <queue>
using namespace std;

// To display a pair.
void displayPair(pair<int, int> pair)
{
// Storing the first and the second elements respectively from the queue of pairs.
int first = pair.first;
int second = pair.second;

// Printing the elements.
cout << "(" << first << "," << second << ") ";
}

// To display the queue of pairs.
void displayQueue(queue<pair<int, int>> queuePair)
{
// To print the elements till the queue becomes empty.
while (!queuePair.empty())
{
displayPair(queuePair.front());
queuePair.pop();
}
cout << "\n";
}

int main()
{
queue<pair<int, int>> queuePair;

int n, a, b;

// Taking user input.
cout << "Enter the number of pairs to be added to the queue: ";
cin >> n;

// Pushing the entered values inside the queue.
cout << "Enter the pairs that need to be stored inside the queue:\n";
for (int i = 0; i < n; i++)
{
cin >> a >> b;
queuePair.push({a, b});
}

cout << "The queue of pairs that has been entered is: ";
displayQueue(queuePair);

// Display the first element of this queue of pairs.
cout << "The first element of this queue of pairs is: ";
displayPair(queuePair.front());

// Display the last element of this queue of pairs.
cout << "The last element of this queue of pairs is: ";
displayPair(queuePair.back());

// Removing the first element and displaying the remaining queue along with its size.
queuePair.pop();
cout << "The size after removing the first pair from the queue: ";
cout << queuePair.size();
cout << "The altered queue becomes: ";
displayQueue(queuePair);

return 0;
}``````

Input

``````Enter the number of pairs to be added to the queue: 5
Enter the pairs that need to be stored inside the queue:
1 2
3 4
5 6
7 8
9 10``````

Output

``````The queue of pairs that has been entered is: (1,2) (3,4) (5,6) (7,8) (9,10)
The first element of this queue of pairs is: (1,2)
The last element of this queue of pairs is: (9,10)
The size after removing the first pair from the queue: 4
The altered queue becomes: (3,4) (5,6) (7,8) (9,10) ``````

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is a pair data type?

The Pair Data type can also be thought of as an array of size two but both the elements can have a different data type. In C++, the elements can be accessed like variableName.first and variableName.second.

What is the FIFO rule?

The FIFO rule means that the element that enters a queue first, leaves it first as well. It stands for First in First Out

Conclusion

So, this blog discussed the implementation of the queue of pairs in C++ using STL. To learn more, head over right now to Coding Ninjas Studio to practice problems on topics like Stacks and Queues and crack your interviews like a Ninja!

Practicing a bunch of questions is not enough in this competitive world. So go check out where you stand among your peers by taking our mock tests and see which areas need improvement.