Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Queue C++ STL

Last Updated on June 27, 2024 by Abhishek Sharma

The Standard Template Library (STL) in C++ provides a collection of powerful data structures and algorithms, among which the queue is a fundamental and widely-used container. A queue is a First-In-First-Out (FIFO) data structure, where elements are added at the back and removed from the front. This behavior makes queues ideal for scenarios like task scheduling, buffering, and breadth-first search algorithms. In this guide, we will explore the queue container in the C++ STL, covering its basic operations, usage, and practical examples to help you understand and utilize this essential data structure effectively.

What is a queue in C++ STL?

A queue is a container adapter in the C++ Standard Template Library (STL) that operates on a First-In-First-Out (FIFO) basis. Elements are inserted at the back (end) and removed from the front.

Given below are the operations of the queue:

  • Enqueue: This function is used to add an element to the rear end of the queue. If the queue is completely filled, then it will be in an overflow condition. The time complexity of the enqueue is O(1).

  • Dequeue: This function is used to remove an element from the front end of the queue. If the queue is empty, then it will be in an underflow condition. The time complexity of the dequeue is O(1).

  • Front: This function returns the front element of the queue. The time complexity of this function is O(1).

  • Rear: This function returns the last element of the queue. The time complexity of this function is O(1).

Queue STL:

In C++, the STL queue provides the functionality of a queue data structure.
A Queue is a linear data structure. Queue follows the FIFO rule i.e. First in First out. In other words we can say the element that goes in first is the element that comes out first.
For Example A ticket Queue outside a cinema hall where the person enters the queue first will get the ticket first.

Create STL queue in C++:

  • First we need to include a queue header file.
    #include

  • After importing the header file we’ll create the queue with a suitable data type.
    queue q
    For example:
    queue q;
    queue q1;

C++ queue STL Operations:

push(): This operation is used to insert an element at the end of the queue.

pop(): This operation is used to remove an element from the end of the queue.

front(): This operation is used to return the first element of the queue.

back(): This operation is used to return the last element of the queue.

empty(): This operation is used to check whether the queue is empty or not.

size(): This operation is used to return the size of the queue.

#include <iostream>
#include <queue>

using namespace std;

// Print the queue
void print(queue<int> q)
{
	queue<int> g = q;
	while (!g.empty()) {
		cout << '\t' << g.front();
		g.pop();
	}
	cout << '\n';
}

int main()
{
	queue<int> q;
	q.push(10);
	q.push(20);
	q.push(30);

	cout << "The queue q is : ";
	print(q);

	cout << "\nq.size() : " << q.size();
	cout << "\nq.front() : " << q.front();
	cout << "\nq.back() : " << q.back();

	cout << "\nq.pop() : ";
	q.pop();
	print(q);

	return 0;
}

Output:
The queue q is : 10 20 30
q.size() : 3
q.front() : 10
q.back() : 30
q.pop() : 20 30

Complexity

Time Complexity of various operations:

  • push(): O(1)
  • pop(): O(1)
  • size(): O(1)
  • front(): O(1)
  • back(): O(1)
  • empty(): O(1)

Conclusion
The queue container in the C++ STL is a versatile and efficient tool for managing data in a FIFO manner. By understanding its core operations and usage patterns, you can leverage queues to solve a wide range of programming problems, from task scheduling to algorithm implementation. With its straightforward interface and integration with other STL components, the queue container helps streamline the development process and ensures that your code is both efficient and easy to maintain. Whether you are a novice or an experienced programmer, mastering the queue container will enhance your C++ programming skills and expand your toolkit.

FAQs related to Queue C++ STL

Here are some of the FAQs related to Queue C++ STL:

Q1: What is a queue in C++ STL?
A queue is a container adapter in the C++ Standard Template Library (STL) that operates on a First-In-First-Out (FIFO) basis. Elements are inserted at the back (end) and removed from the front.

Q2: How do you include a queue in a C++ program?
You can include the queue container in your program by including the header file:

#include 

Q3: How do you create a queue in C++?
You can create a queue by declaring an object of the std::queue type. For example:

std::queue myQueue;

Q4: What are the basic operations supported by a queue in C++ STL?
The basic operations supported by a queue include:

  • push(): Adds an element to the back of the queue.
  • pop(): Removes the front element from the queue.
  • front(): Accesses the front element of the queue.
  • back(): Accesses the back element of the queue.
  • empty(): Checks if the queue is empty.
  • size(): Returns the number of elements in the queue.

Q5: How do you add an element to a queue?
A: You can add an element to a queue using the push() method. For example:

std::queue myQueue;
myQueue.push(10); // Adds the element 10 to the queue

Leave a Reply

Your email address will not be published. Required fields are marked *