
Introduction
A 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 deque, priority 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)
Read about Application of Queue in Data Structure here.