Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Iterate a STL Queue in C++?
Next article icon

Queue in C++ STL

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the data structure.

Example:

C++
#include <iostream> #include <queue> using namespace std;  int main() {          // Creating a queue of integers     queue<int> q;      // Pushing elements into the queue     q.push(3);     q.push(4);     q.push(5);      while (!q.empty()) {         cout << q.front() << " ";         q.pop();     }     return 0; } 

Output
3 4 5 

Explanation: In this program, we created a queue of integers and pushed three elements {3, 4, 5} into it.

Syntax

Queue is defined as the std::queue class template inside <queue> header file.

queue<T> q;

where,

  • T: Type of elements in the queue.
  • q: Name assigned to the queue.

Declaration and Initialization

In C++, queue can be declared and initialized in multiple ways as shown in the below example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {          // Declare empty queue     queue<int> q1;          // Insert some elements into queue     q1.push(3);     q1.push(4);     q1.push(5);          // Create another queue from q1     queue<int> q2(q1);          while (!q2.empty()) {         cout << q2.front() << " ";         q2.pop();     }     return 0; } 

Output
3 4 5 

Explanation: In the above program,

  • Statement queue<int> q1 creates an empty queue of integers. Elements are later added into it.
  • Statement queue<int> q2(q1) creates another queue q2 with same elements as q1.

Basic Operations

Here are the basic operations that can be performed on a queue:

1. Inserting Elements

New elements can only be inserted at back of the queue using push() function. The process of inserting elements in a queue is also called enqueue.

Example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {     queue<int> q;      // Pushing elements into the queue     q.push(3);     q.push(4);     q.push(5);          while (!q.empty()) {         cout << q.front() << " ";         q.pop();     }     return 0; } 

Output
3 4 5 

2. Accessing Elements

Only the front and back elements of the queue can be accessed by using front() and back() functions respectively. We can’t access any middle element of queue.

Example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {     queue<int> q;     q.push(3);     q.push(4);     q.push(5);      // Accessing the front and back elements     cout << q.front() << endl;     cout << q.back();     return 0; } 

Output
3 5 

3. Deleting Elements

Elements can only be deleted from the front of the queue using the pop() function. This operation is also called dequeue.

C++
#include <bits/stdc++.h> using namespace std;  int main() {     queue<int> q;     q.push(3);     q.push(4);     q.push(5);          // Deleting elements from front side     // of the queue     q.pop();          while (!q.empty()) {         cout << q.front() << " ";         q.pop();     }     return 0; } 

Output
4 5

4. Pseudo Traversal

Since only the front and back element can be accessed in a queue, we cannot directly traverse it. On the other hand, we can create a copy of the queue, access the front element, and then delete it. By continuing this process until the copied queue is empty, we can effectively traverse all the elements of the queue.

Example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {     queue<int> q;     q.push(3);     q.push(4);     q.push(5);          // Create a copy     queue<int> temp(q);          while(!temp.empty()) {         cout << temp.front() << " ";         temp.pop();     }     return 0; } 

Output
3 4 5 

Time Complexity

The below table lists the time complexity of the basic operations on a queue:

OperationTime Complexity
Insert an elementO(1)
Delete an elementO(1)

Access front element

O(1)

Access back elementO(1)
Traverse the queueO(n)

Other Common Operations

The following are some more operations on queue that will help you become more familiarize with this container:

  • Find the Size of Queue
  • Check if the Queue is Empty
  • Swap Two Queue
  • Reverse a Queue

Internal Working of Queue

Queue container provides the built-in implementation of the Queue data structure in C++. It is a container adaptor that is built over another container like deque or list. The queue follows the FIFO (First In, First Out) principle to insert and delete elements. This behaviour is achieved using underlying data structures that allow constant time insert and delete operations at both ends.

Example, we cannot use vector as it does not allow constant time for deletion at both ends. But on the other hand, deque allows the constant time insertion at the back and deletion at the front. All the other operations of deque are restricted.

All Member Functions

Following is the list of all member functions of std::queue class in C++:

Functions

Description

front()

Access the front element of the queue.

back()

Access the end element of the queue.

empty()

Check whether a queue is empty or not.

size()

Returns the number of elements in the queue.

push()

Adding an element at the back of the queue.

push_range()

Adding multiple elements at the end of queue.

emplace()

Add the constructs element in top of the queue.

pop()

Delete the front element of the queue.

swap()

Swap two queues.


Next Article
How to Iterate a STL Queue in C++?
author
kartik
Improve
Article Tags :
  • C++
  • cpp-containers-library
  • cpp-queue
  • STL
Practice Tags :
  • CPP
  • STL

Similar Reads

  • Queue in C++ STL
    In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat
    5 min read
  • How to Iterate a STL Queue in C++?
    A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Syntax: queue<datatype> queuename;Datatype: Queue can take any data type depending on the values, e.g. int, char, float, etc. The std: :queue container d
    4 min read
  • queue push() and pop() in C++ STL
    The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file. In this art
    2 min read
  • queue::front() and queue::back() in C++ STL
    Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::front() This function is used to reference the first or the oldest element of the queue container. This function can
    3 min read
  • queue::empty() and queue::size() in C++ STL
    Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis
    4 min read
  • queue::emplace() in C++ STL
    Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::emplace() This fu
    3 min read
  • queue::swap() in C++ STL
    Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() fun
    2 min read
  • Queue of Pairs in C++ STL with Examples
    Queue in STL are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement where elements are inserted at the back (end) and are deleted from the front. Queue of pair can be very efficient in designing complex data structures. The first element is referenced as ‘f
    2 min read
  • Queue using Stacks
    Given a stack that supports push and pop operations, your task is to implement a queue using one or more instances of that stack along with its operations. Table of Content By Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Makin
    11 min read
  • Implement thread-safe queue in C++
    What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchr
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences