Javatpoint Logo
Javatpoint Logo

Queue of Pairs in C++ STL with Examples

Introduction

In this article, we investigated the idea of a queue of pairs in C++ STL through an extensive example. By joining queues and pairs, we can effectively oversee collections of related information while safeguarding their order and association. The provided code examples exhibit different tasks, for example, enqueuing, dequeuing, accessing components, and handling the queue. This flexible data structure can be adjusted to a great many situations where maintaining sets of data elements is fundamental for effective programming.

What is Standard Template Library?

In C++ programming, the Standard Template Library gives a different array of data structures and algorithms for effective programming.Among these, queues and pairs are crucial parts habitually utilized in different situations. Consolidating them considers the formation of a queue of pairs, which can be especially helpful in circumstances where components should be handled in a first-in-first-out (FIFO) way while keeping up with related key-value pairs. In this article, we'll dive into the idea of a queue of pairs in C++ STL, giving extensive clarifications and examples to aid understanding.

Understanding Queue of Pairs

Before we jump into code examples, we should embrace the idea of a queue of pairs. A queue is a data structure that follows the FIFO principle, meaning the first component embedded is the first to be eliminated. Then again, a pair is a simple container that holds two heterogeneous items as a single element. Combining these two ideas, a queue of pairs permits us to enqueue and dequeue pairs of components, with each pair holding its order and integrity inside the queue.

Example Scenario

Assume we have a situation where we need to process tasks addressed by pairs of integers and characters. Each pair signifies a task ID (number) and its corresponding priority (character). We need to enqueue these tasks and then dequeue and process them in view of their priority order.

Code

Output:

Queue of Pairs in C++ STL with Examples

Code Explanation

Including Necessary Headers

  • The code incorporates three standard C++ headers: <iostream> for input/output tasks, <queue> for utilizing the queue container, and <utility> for using the std::pair utility.

Main Function

  • The main() function fills in as the entry point of the program.

Queue Initialization

  • A queue named studentQueue is characterized by holding pairs of numbers and floating-point numbers, addressing student IDs and GPAs separately. It is of type std::queue<std::pair<int, float>>.

Enqueuing Student Records

  • Three student records are enqueued into the studentQueue utilizing the push() function. Each record is made utilizing std::make_pair() to make a pair of student ID and GPA.

Printing Queue Size

  • The ongoing size of the queue is printed to the control center utilizing studentQueue.size().

Accessing Elements without Dequeuing

  • The ID and GPA of the first student record in the queue are gotten to and printed without dequeuing the record. This is accomplished by utilizing studentQueue.front() to get to the front element of the queue.

Dequeuing

  • The first student record is dequeued from the queue utilizing studentQueue.pop(). This eliminates the element from the front of the queue.

Printing Queue Size After Dequeue

  • The size of the queue after the dequeue activity is printed to the console to exhibit the change in size.

Enqueuing Additional Student Records

  • Two more student records are enqueued into the studentQueue after the dequeue activity, utilizing a similar push() strategy.

Processing and Printing Student Records

  • A while loop repeats through the queue until it becomes empty (studentQueue.empty()). Inside the loop, every student record is recovered from the front of the queue utilizing studentQueue.front(), printed, and afterward dequeued utilizing studentQueue.pop().
  • The program finishes up by returning 0, showing effective execution. Overall, this code represents how to use a queue of pairs in C++ STL to oversee and deal with collections of related data proficiently.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA