Queue Of Pairs In C++ STL With Examples - C++ Programming Tutorial
C++ Course / Data Structures / Queue Of Pairs In C++ STL With Examples

Queue Of Pairs In C++ STL With Examples

BLUF: Mastering Queue Of Pairs In C++ STL With Examples is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Queue Of Pairs In C++ STL With Examples

C++ is renowned for its efficiency. Learn how Queue Of Pairs In C++ STL With Examples enables low-level control and high-performance computing in the tutorial below.

Introduction

In this guide, we explored the concept of a pair queue in C++ STL using a detailed illustration. By combining queues and pairs, we can efficiently manage groups of interconnected data while preserving their sequence and relationships. The included code snippets demonstrate various operations such as adding to the queue, removing from it, accessing elements, and managing the queue. This versatile data structure can be tailored to numerous scenarios where organizing data sets is crucial for successful programming.

What is Standard Template Library?

In C++ development, the Standard Template Library offers a diverse range of data structures and algorithms for efficient programming. Within these offerings, queues and pairs play significant roles and are commonly employed in various scenarios. Combining these two elements involves creating a queue of pairs, which proves to be particularly advantageous in situations where elements need to be managed in a first-in-first-out (FIFO) manner while preserving associated key-value pairs. This tutorial delves into the concept of a queue of pairs in C++ STL, providing detailed explanations and illustrative examples to facilitate comprehension.

Understanding Queue of Pairs

Before delving into code illustrations, it's essential to grasp the concept of a pair queue. A queue adheres to the FIFO principle, ensuring that the initial element added is the first one to be removed. On the other hand, a pair serves as a basic container that stores two distinct items as a unified entity. By merging these concepts, a pair queue enables us to add and remove pairs of elements, with each pair preserving its sequence and unity within the queue.

Example Scenario

In a scenario where tasks are associated with pairs of integers and characters, each pair represents a task ID (integer) and its priority (character). The tasks are queued and subsequently dequeued for processing based on their priority sequence.

Example

#include <iostream>
#include <queue>
#include <utility>
int main() {
    std::queue<std::pair<int, float>>studentQueue;
studentQueue.push(std::make_pair(101, 3.8f)); 
studentQueue.push(std::make_pair(102, 3.5f)); 
studentQueue.push(std::make_pair(103, 3.9f)); 

    std::cout<< "Size of the student queue: " <<studentQueue.size() << std::endl;
    std::cout<< "First student record: ID = " <<studentQueue.front().first 
<< ", GPA = " <<studentQueue.front().second << std::endl;
studentQueue.pop();
    std::cout<< "Size of the student queue after dequeue: " <<studentQueue.size() << std::endl;
studentQueue.push(std::make_pair(104, 3.7f)); 
studentQueue.push(std::make_pair(105, 3.6f)); 
    std::cout<< "Student records:" << std::endl;
    while (!studentQueue.empty()) {
        std::pair<int, float> student = studentQueue.front();
        std::cout<< "Student ID: " <<student.first<< ", GPA: " <<student.second<< std::endl;
studentQueue.pop();
    }
    return 0;
}

Output:

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.

The main function serves as the starting point of the program's execution.

Initialize a queue named studentQueue to store pairs of integers and floating-point numbers representing student IDs and GPAs respectively. This queue is defined as std::queue<std::pair<int, float>>.

Enqueuing Student Data

  • Three data entries for students are added to the studentQueue using the push method. Every entry is created by employing std::make_pair to generate a combination of student ID and GPA.

Displaying Queue Capacity

The current capacity of the queue is shown on the console using studentQueue.size.

Accessing Items without Removing

To retrieve and display the ID and GPA of the initial student entry in the queue without removing it, you can use studentQueue.front to access the front item in the queue.

Dequeuing

  • The initial student entry is removed from the queue by using studentQueue.pop. This action deletes the item from the beginning of the queue.

Displaying Queue Size Post Dequeue Operation

The queue's current size is output to the console after the dequeue operation to demonstrate the alteration in size.

Adding More Student Entries

  • Following the dequeue operation, two extra student entries are added to the studentQueue using the same push technique.

Iterating Through and Displaying Student Information

A while loop iterates over the queue until it is no longer populated (studentQueue.empty). Within the loop, each student's details are retrieved from the front of the queue using studentQueue.front, displayed, and then removed from the queue using studentQueue.pop.

Concluding the program by returning 0 indicates successful completion. In summary, this example illustrates the utilization of a pair queue in C++ STL for efficient management of interconnected data sets.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience