ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.programiz.com/cpp-programming/queue |
| Last Crawled | 2026-04-14 22:19:14 (3 days ago) |
| First Indexed | 2022-01-03 23:41:44 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | C++ Queue (With Examples) |
| Meta Description | The C++ STL queue provides the functionality of a queue data structure. In this tutorial, you will learn about the C++ queue and its various operations in C++ with the help of examples. |
| Meta Canonical | null |
| Boilerpipe Text | In C++, the STL
queue
provides the functionality of a queue data structure.
The queue data structure follows the
FIFO (First In First Out)
principle where elements that are added first will be removed first.
Queue Data Structure
Recommended Readings
Queue Data Structure
C++ STL
Create C++ STL Queue
In order to create a queue in C++, we first need to include the
queue
header file.
#include <queue>
Once we import this file, we can create a
queue
using the following syntax:
queue<type> q;
Here,
type
indicates the
data type
we want to store in the queue. For example,
// create a queue of integer data type
queue<int> integer_queue;
// create a queue of string data type
queue<string> string_queue;
C++ Queue Methods
In C++,
queue
is a
class
that provides various methods to perform different operations on a queue.
Methods
Description
push()
Inserts an element at the back of the queue.
pop()
Removes an element from the front of the queue.
front()
Returns the first element of the queue.
back()
Returns the last element of the queue.
size()
Returns the number of elements in the queue.
empty()
Returns
true
if the queue is empty.
Insert Element to a Queue
We use the
push()
method to insert an element to the back of a queue. For example,
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> animals;
// push elements into the queue
animals.push("Cat");
animals.push("Dog");
cout << "Queue: ";
// print elements of queue
// loop until queue is empty
while(!animals.empty()) {
// print the element
cout << animals.front() << ", ";
// pop element from the queue
animals.pop();
}
cout << endl;
return 0;
}
Output
Queue: Cat, Dog,
In the above example, we have created a
queue
of
strings
called
animals
. Here, we have used the
push()
method to add elements to the end of the queue.
animals.push("Cat");
animals.push("Dog");
Instead of directly printing the contents of the queue, we have used a
while
loop and various queue methods.
while(!animals.empty()) {
cout << animals.front() << ", ";
animals.pop();
}
This is because the STL queue is an
STL Container Adapter
that provides restrictive access to make it behave like a standard queue data structure.
As a result, we cannot iterate through the queue like iterating through
vectors
or other
containers
.
Instead, we print its
front
and then
pop
the element repeatedly inside a loop until the queue is empty.
We will learn about
pop()
,
front()
and
empty()
in the coming sections.
Remove Element from a Queue
We can use the
pop()
method to remove an element from the front of a queue. For example,
#include <iostream>
#include <queue>
using namespace std;
// function prototype for display_queue utility
void display_queue(queue<string> q);
int main() {
// create a queue of string
queue<string> animals;
// push element into the queue
animals.push("Cat");
animals.push("Dog");
animals.push("Fox");
cout << "Initial Queue: ";
display_queue(animals);
// remove element from queue
animals.pop();
cout << "Final Queue: ";
display_queue(animals);
return 0;
}
// utility function to display queue
void display_queue(queue<string> q) {
while(!q.empty()) {
cout << q.front() << ", ";
q.pop();
}
cout << endl;
}
Output
Initial Queue: Cat, Dog, Fox,
Final Queue: Dog, Fox,
In the above example, we have used the
pop()
method to remove an element from the queue.
Initially, the contents of the queue are
"Cat"
,
"Dog"
and
"Fox"
.
// removes front element
animals.pop();
Here
animals.pop()
removes the element at the front of the queue i.e. the element inserted at the very beginning which is
"Cat"
.
Hence, the final queue contains the elements
"Dog"
and
"Fox"
.
Access Element from a Queue
We can access the elements of a
queue
using the following methods:
front()
- returns the element from the front of the queue
back()
- returns the element from the back of the queue
For example,
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of int
queue<int> nums;
// push element into the queue
nums.push(1);
nums.push(2);
nums.push(3);
// get the element at the front
int front = nums.front();
cout << "First element: " << front << endl;
// get the element at the back
int back = nums.back();
cout << "Last element: " << back << endl;
return 0;
}
Output
First element: 1
Last element: 3
In the above example, we have used the
front()
and
back()
methods to get the first and last elements from a queue of integers called
nums
.
We can get the first element i.e. the element at the front using:
// returns 1
nums.front()
Here,
1
was inserted first so it is at the front.
Similarly, we find the last element i.e. the rear (back) element using:
// returns 3
nums.back()
Here,
3
was inserted last, so it is at the back.
Get the size of a Queue
We use the
size()
method to get the number of elements in the
queue
. For example,
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> languages;
// push element into the queue
languages.push("Python");
languages.push("C++");
languages.push("Java");
// get the size of the queue
int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}
Output
Size of the queue: 3
In the above example, we have created a queue of strings called
languages
and added three elements to it.
Then, we used the
size()
method to find the number of elements in the queue:
// returns 3
languages.size();
Since we have added three elements to the queue,
languages.size()
returns
3
.
Check if the Queue is Empty
We use the
empty()
method to check if the
queue
is empty. This method returns:
1 (true)
- if the queue is empty
0 (false)
- if the queue is not empty
For example,
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> languages;
cout << "Is the queue empty? ";
// check if the queue is empty
if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the queue
languages.push("Python");
languages.push("C++");
cout << "Is the queue empty? ";
// check if the queue is empty
if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Output
Is the queue empty? Yes
Pushing elements...
Is the queue empty? No
In the above example, we have used the
empty()
method to determine if the
queue
is empty,
if (languages.empty()) {
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
Initially, the queue has no elements in it. So
languages.empty()
returns
true
.
We then add elements to the queue.
Then we again use
languages.empty()
again to determine if the queue is empty. This time, it returns
false
.
Also Read
C++ Priority Queue
C++ Deque
C++ Stack |
| Markdown | 
[Stop copy pasting code you don't actually understand Build the coding confidence you need to become a developer companies will fight for Stop copy pasting code you don't actually understand Ends in Start FREE Trial Start FREE Trial Start FREE Trial Start FREE Trial](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=programiz&utm_content=default_banner&utm_term=sticky_banner_launch "Start FREE Trial")
[Stop copy pasting code you don't actually understand Build the coding confidence you need to become a developer companies will fight for Stop copy pasting code you don't actually understand Ends in Start FREE Trial Start FREE Trial Start FREE Trial Start FREE Trial](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=programiz&utm_content=default_banner&utm_term=sticky_banner_launch "Start FREE Trial")
[ ](https://www.programiz.com/ "Programiz")
[Tutorials](https://www.programiz.com/cpp-programming/queue "Programming Tutorials")
[Examples]("Programming Examples")
[ Courses](https://www.programiz.com/cpp-programming/queue "Learn to Code Interactively")
[Try Programiz PRO](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button "Try Programiz PRO")
Course Index
Tutorials
Courses
[Python]("Python") [JavaScript]("JavaScript") [TypeScript]("TypeScript") [SQL]("SQL") [HTML]("HTML") [CSS]("CSS") [C]("C") [C++]("C++") [Java]("Java") [R]("R") [Ruby]("Ruby") [RUST]("RUST") [Golang]("Golang") [Kotlin]("Kotlin") [Swift]("Swift") [C\#]("C#") [DSA]("DSA")
Become a certified C++
programmer.
[ENROLL](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner)
#### Popular Tutorials
[C++ if...else Statement](https://www.programiz.com/cpp-programming/if-else?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ if...else Statement")
[C++ for Loop](https://www.programiz.com/cpp-programming/for-loop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ for Loop")
[Arrays in C++](https://www.programiz.com/cpp-programming/arrays?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Arrays in C++")
[Strings in C++](https://www.programiz.com/cpp-programming/strings?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Strings in C++")
[C++ Class & Objects](https://www.programiz.com/cpp-programming/object-class?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Class & Objects")
[Start Learning C++](https://www.programiz.com/cpp-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Tutorials")
#### Popular Examples
[Create a simple calculator](https://www.programiz.com/cpp-programming/examples/calculator-switch-case?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Create a simple calculator")
[Check prime number](https://www.programiz.com/cpp-programming/examples/prime-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Check prime number")
[Print the Fibonacci sequence](https://www.programiz.com/cpp-programming/examples/fibonacci-series?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Print the Fibonacci sequence")
[Check if a number is palindrome or not](https://www.programiz.com/cpp-programming/examples/palindrome-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Check if a number is palindrome or not")
[Program to multiply matrix](https://www.programiz.com/cpp-programming/examples/matrix-multiplication?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Program to multiply matrix")
[Explore C++ Examples](https://www.programiz.com/cpp-programming/examples?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Examples")
#### Reference Materials
[iostream](https://www.programiz.com/cpp-programming/library-function/iostream?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "iostream")
[cmath](https://www.programiz.com/cpp-programming/library-function/cmath?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cmath")
[cstring](https://www.programiz.com/cpp-programming/library-function/cstring?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cstring")
[ctime](https://www.programiz.com/cpp-programming/library-function/ctime?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "ctime")
[View all](https://www.programiz.com/cpp-programming/library-function?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ References")

Created with over a decade of experience.
- [Learn]("Learn")
- [Practice]("Practice")
- [Compete]("Compete")
[Learn Python](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_courses "Learn Python")
[Learn HTML](https://programiz.pro/course/learn-html?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_html&utm_term=nav_programiz-pro_courses "Learn HTML")
[Learn JavaScript](https://programiz.pro/learn/master-javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_courses "Learn JavaScript")
[Learn SQL](https://programiz.pro/course/learn-sql-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_sql&utm_term=nav_programiz-pro_courses "Learn SQL")
[Learn DSA](https://programiz.pro/course/dsa-with-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_dsa&utm_term=nav_programiz-pro_courses "Learn DSA")
[Learn C](https://programiz.pro/learn/master-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_courses "Learn C")
[Learn C++](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_courses "Learn C++")
[Learn Java](https://programiz.pro/learn/master-java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_courses "Learn Java")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Python Basics](https://programiz.pro/course/practice-python-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_practice_courses "Python Basics")
[Python Intermediate](https://programiz.pro/course/practice-python-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_practice_courses "Python Intermediate")
[C++ Basics](https://programiz.pro/course/practice-cpp-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ Basics")
[C++ Intermediate](https://programiz.pro/course/practice-cpp-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ Intermediate")
[C++ OOP](https://programiz.pro/course/practice-cpp-oop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ OOP")
[C Programming](https://programiz.pro/course/practice-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_practice_courses "C Programming")
[Java Basics](https://programiz.pro/course/practice-java-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java Basics")
[Java Intermediate](https://programiz.pro/course/practice-java-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java Intermediate")
[Java OOP](https://programiz.pro/course/practice-java-oop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java OOP")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Python Challenges](https://programiz.pro/community-challenges/python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_challenges "Python Challenges")
[JavaScript Challenges](https://programiz.pro/community-challenges/javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_challenges "JavaScript Challenges")
[Java Challenges](https://programiz.pro/community-challenges/java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_challenges "Java Challenges")
[C++ Challenges](https://programiz.pro/community-challenges/cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_challenges "C++ Challenges")
[C Challenges](https://programiz.pro/community-challenges/c?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_challenges "C Challenges")
[View all Challenges on ](https://programiz.pro/community-challenges?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Challenges on PRO")
[Learn]()
[Practice]()
[Compete]()
#### Certification Courses
Created with over a decade of experience and thousands of feedback.
[Learn Python](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_challenges "Learn Python")
[Learn HTML](https://programiz.pro/course/learn-html?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_html&utm_term=nav_programiz-pro_challenges "Learn HTML")
[Learn JavaScript](https://programiz.pro/learn/master-javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_challenges "Learn JavaScript")
[Learn SQL](https://programiz.pro/course/learn-sql-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_sql&utm_term=nav_programiz-pro_challenges "Learn SQL")
[Learn DSA](https://programiz.pro/course/dsa-with-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_dsa&utm_term=nav_programiz-pro_challenges "Learn DSA")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Learn C](https://programiz.pro/learn/master-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_challenges "Learn C")
[Learn C++](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_challenges "Learn C++")
[Learn Java](https://programiz.pro/learn/master-java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_challenges "Learn Java")
[Python]("Python")
[JavaScript]("JavaScript")
[TypeScript]("TypeScript")
[SQL]("SQL")
[HTML]("HTML")
[CSS]("CSS")
[C]("C")
[C++]("C++")
[Java]("Java")
[More languages]("More languages")
### Become a certified C++ programmer.
[Try Programiz PRO\!](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner)
#### Popular Tutorials
[C++ if...else Statement](https://www.programiz.com/cpp-programming/if-else?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ if...else Statement")
[C++ for Loop](https://www.programiz.com/cpp-programming/for-loop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ for Loop")
[Arrays in C++](https://www.programiz.com/cpp-programming/arrays?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Arrays in C++")
[Strings in C++](https://www.programiz.com/cpp-programming/strings?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Strings in C++")
[C++ Class & Objects](https://www.programiz.com/cpp-programming/object-class?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Class & Objects")
[Start Learning C++](https://www.programiz.com/cpp-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ tutorials")
[All C++ Tutorials](https://www.programiz.com/cpp-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "All C++ tutorials")
#### Reference Materials
[iostream](https://www.programiz.com/cpp-programming/library-function/iostream?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "iostream")
[cmath](https://www.programiz.com/cpp-programming/library-function/cmath?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cmath")
[cstring](https://www.programiz.com/cpp-programming/library-function/cstring?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cstring")
[ctime](https://www.programiz.com/cpp-programming/library-function/ctime?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "ctime")
[View all](https://www.programiz.com/cpp-programming/library-function?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ References")
[Python]("Python")
[JavaScript]("JavaScript")
[C]("C")
[C++]("C++")
[Java]("Java")
[R]("R")
[Kotlin]("Kotlin")
Become a certified C++
programmer.
[Try Programiz PRO\!](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner)
#### Popular Examples
[Create a simple calculator](https://www.programiz.com/cpp-programming/examples/calculator-switch-case?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Create a simple calculator")
[Check prime number](https://www.programiz.com/cpp-programming/examples/prime-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Check prime number")
[Print the Fibonacci sequence](https://www.programiz.com/cpp-programming/examples/fibonacci-series?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Print the Fibonacci sequence")
[Check if a number is palindrome or not](https://www.programiz.com/cpp-programming/examples/palindrome-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Check if a number is palindrome or not")
[Program to multiply matrix](https://www.programiz.com/cpp-programming/examples/matrix-multiplication?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Program to multiply matrix")
[All C++ Examples](https://www.programiz.com/cpp-programming/examples?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "C++ Examples")
- ### Introduction to C++
- [Getting Started With C++](https://www.programiz.com/cpp-programming/getting-started "Getting Started With C++")
- [Your First C++ Program](https://www.programiz.com/cpp-programming/first-program "Your First C++ Program")
- [C++ Comments](https://www.programiz.com/cpp-programming/comments "C++ Comments")
- ### C++ Fundamentals
- [C++ Keywords and Identifiers](https://www.programiz.com/cpp-programming/keywords-identifiers "C++ Keywords and Identifiers")
- [C++ Variables, Literals and Constants](https://www.programiz.com/cpp-programming/variables-literals "C++ Variables, Literals and Constants")
- [C++ Data Types](https://www.programiz.com/cpp-programming/data-types "C++ Data Types")
- [C++ Type Modifiers](https://www.programiz.com/cpp-programming/type-modifiers "C++ Type Modifiers")
- [C++ Constants](https://www.programiz.com/cpp-programming/constants "C++ Constants")
- [C++ Basic Input/Output](https://www.programiz.com/cpp-programming/input-output "C++ Basic Input/Output")
- [C++ Operators](https://www.programiz.com/cpp-programming/operators "C++ Operators")
- ### Flow Control
- [C++ Relational and Logical Operators](https://www.programiz.com/cpp-programming/relational-logical-operators "C++ Relational and Logical Operators")
- [C++ if, if...else and Nested if...else](https://www.programiz.com/cpp-programming/if-else "C++ if, if...else and Nested if...else")
- [C++ for Loop](https://www.programiz.com/cpp-programming/for-loop "C++ for Loop")
- [C++ while and do...while Loop](https://www.programiz.com/cpp-programming/do-while-loop "C++ while and do...while Loop")
- [C++ break Statement](https://www.programiz.com/cpp-programming/break-statement "C++ break Statement")
- [C++ continue Statement](https://www.programiz.com/cpp-programming/continue-statement "C++ continue Statement")
- [C++ goto Statement](https://www.programiz.com/cpp-programming/goto "C++ goto Statement")
- [C++ switch..case Statement](https://www.programiz.com/cpp-programming/switch-case "C++ switch..case Statement")
- [C++ Ternary Operator](https://www.programiz.com/cpp-programming/ternary-operator "C++ Ternary Operator")
- ### Functions
- [C++ Functions](https://www.programiz.com/cpp-programming/function "C++ Functions")
- [C++ Programming Default Arguments](https://www.programiz.com/cpp-programming/default-argument "C++ Programming Default Arguments")
- [C++ Function Overloading](https://www.programiz.com/cpp-programming/function-overloading "C++ Function Overloading")
- [C++ Inline Functions](https://www.programiz.com/cpp-programming/inline-function "C++ Inline Functions")
- [C++ Recursion](https://www.programiz.com/cpp-programming/recursion "C++ Recursion")
- ### Arrays and Strings
- [C++ Arrays](https://www.programiz.com/cpp-programming/arrays "C++ Arrays")
- [C++ Array to Function](https://www.programiz.com/cpp-programming/passing-arrays-function "C++ Array to Function")
- [C++ Multidimensional Arrays](https://www.programiz.com/cpp-programming/multidimensional-arrays "C++ Multidimensional Arrays")
- [C++ String](https://www.programiz.com/cpp-programming/strings "C++ String")
- [C++ String Class](https://www.programiz.com/cpp-programming/string-class "C++ String Class")
- ### Pointers and References
- [C++ Pointers](https://www.programiz.com/cpp-programming/pointers "C++ Pointers")
- [C++ Pointers and Arrays](https://www.programiz.com/cpp-programming/pointers-arrays "C++ Pointers and Arrays")
- [C++ References: Using Pointers](https://www.programiz.com/cpp-programming/references "C++ References: Using Pointers")
- [C++ Call by Reference: Using pointers](https://www.programiz.com/cpp-programming/pointers-function "C++ Call by Reference: Using pointers")
- [C++ Memory Management: new and delete](https://www.programiz.com/cpp-programming/memory-management "C++ Memory Management: new and delete")
- ### Structures and Enumerations
- [C++ Structures](https://www.programiz.com/cpp-programming/structure "C++ Structures")
- [C++ Structure and Function](https://www.programiz.com/cpp-programming/structure-function "C++ Structure and Function")
- [C++ Pointers to Structure](https://www.programiz.com/cpp-programming/structure-pointer "C++ Pointers to Structure")
- [C++ Enumeration](https://www.programiz.com/cpp-programming/enumeration "C++ Enumeration")
- ### Object Oriented Programming
- [C++ Classes and Objects](https://www.programiz.com/cpp-programming/object-class "C++ Classes and Objects")
- [C++ Constructors](https://www.programiz.com/cpp-programming/constructors "C++ Constructors")
- [C++ Constructor Overloading](https://www.programiz.com/cpp-programming/constructor-overloading "C++ Constructor Overloading")
- [C++ Destructors](https://www.programiz.com/cpp-programming/destructors "C++ Destructors")
- [C++ Access Modifiers](https://www.programiz.com/cpp-programming/access-modifiers "C++ Access Modifiers")
- [C++ Encapsulation](https://www.programiz.com/cpp-programming/encapsulation "C++ Encapsulation")
- [C++ friend Function and friend Classes](https://www.programiz.com/cpp-programming/friend-function-class "C++ friend Function and friend Classes")
- ### Inheritance & Polymorphism
- [C++ Inheritance](https://www.programiz.com/cpp-programming/inheritance "C++ Inheritance")
- [C++ Public, Protected and Private Inheritance](https://www.programiz.com/cpp-programming/public-protected-private-inheritance "C++ Public, Protected and Private Inheritance")
- [C++ Multiple, Multilevel and Hierarchical Inheritance](https://www.programiz.com/cpp-programming/multilevel-multiple-inheritance "C++ Multiple, Multilevel and Hierarchical Inheritance")
- [C++ Function Overriding](https://www.programiz.com/cpp-programming/function-overriding "C++ Function Overriding")
- [C++ Virtual Functions](https://www.programiz.com/cpp-programming/virtual-functions "C++ Virtual Functions")
- [C++ Abstract Class and Pure Virtual Function](https://www.programiz.com/cpp-programming/pure-virtual-funtion "C++ Abstract Class and Pure Virtual Function")
- ### STL - Vector, Queue & Stack
- [C++ Standard Template Library](https://www.programiz.com/cpp-programming/standard-template-library "C++ Standard Template Library")
- [C++ STL Containers](https://www.programiz.com/cpp-programming/stl-containers "C++ STL Containers")
- [C++ std::array](https://www.programiz.com/cpp-programming/std-array "C++ std::array")
- [C++ Vectors](https://www.programiz.com/cpp-programming/vectors "C++ Vectors")
- [C++ List](https://www.programiz.com/cpp-programming/list "C++ List")
- [C++ Forward List](https://www.programiz.com/cpp-programming/forward-list "C++ Forward List")
- [C++ Queue](https://www.programiz.com/cpp-programming/queue "C++ Queue")
- [C++ Deque](https://www.programiz.com/cpp-programming/deque "C++ Deque")
- [C++ Priority Queue](https://www.programiz.com/dsa/priority-queue "C++ Priority Queue")
- [C++ Stack](https://www.programiz.com/cpp-programming/stack "C++ Stack")
- ### STL - Map & Set
- [C++ Map](https://www.programiz.com/cpp-programming/map "C++ Map")
- [C++ Set](https://www.programiz.com/cpp-programming/set "C++ Set")
- [C++ Multimap](https://www.programiz.com/cpp-programming/multimap "C++ Multimap")
- [C++ Multiset](https://www.programiz.com/cpp-programming/multiset "C++ Multiset")
- [C++ Unordered Map](https://www.programiz.com/cpp-programming/unordered-map "C++ Unordered Map")
- [C++ Unordered Set](https://www.programiz.com/cpp-programming/unordered-set "C++ Unordered Set")
- [C++ Unordered Multiset](https://www.programiz.com/cpp-programming/unordered-multiset "C++ Unordered Multiset")
- [C++ Unordered Multimap](https://www.programiz.com/cpp-programming/unordered-multimap "C++ Unordered Multimap")
- ### STL - Iterators & Algorithms
- [C++ Iterators](https://www.programiz.com/cpp-programming/iterators "C++ Iterators")
- [C++ Algorithm](https://www.programiz.com/cpp-programming/algorithm "C++ Algorithm")
- [C++ Functor](https://www.programiz.com/cpp-programming/functors "C++ Functor")
- ### Additional Topics
- [C++ Exceptions Handling](https://www.programiz.com/cpp-programming/exception-handling "C++ Exceptions Handling (With Examples)")
- [C++ File Handling](https://www.programiz.com/cpp-programming/file-handling "C++ File Handling")
- [C++ Ranged for Loop](https://www.programiz.com/cpp-programming/ranged-for-loop "C++ Ranged for Loop")
- [C++ Nested Loop](https://www.programiz.com/cpp-programming/nested-loops "C++ Nested Loop")
- [C++ Function Template](https://www.programiz.com/cpp-programming/function-template "C++ Function Template")
- [C++ Class Templates](https://www.programiz.com/cpp-programming/class-templates "C++ Class Templates")
- [C++ Type Conversion](https://www.programiz.com/cpp-programming/type-conversion "C++ Type Conversion")
- [C++ Type Conversion Operators](https://www.programiz.com/cpp-programming/type-conversion-operators "C++ Type Conversion Operators")
- [C++ Operator Overloading](https://www.programiz.com/cpp-programming/operator-overloading "C++ Operator Overloading")
- ### Advanced Topics
- [C++ 11](https://www.programiz.com/cpp-programming/cpp-11 "C++ 11")
- [C++ Lambda](https://www.programiz.com/cpp-programming/lambda-expression "C++ Lambda")
- [C++ Namespaces](https://www.programiz.com/cpp-programming/namespaces "C++ Namespaces")
- [C++ Preprocessors and Macros](https://www.programiz.com/cpp-programming/preprocessor-macros "C++ Preprocessors and Macros")
- [C++ Storage Class](https://www.programiz.com/cpp-programming/storage-class "C++ Storage Class")
- [C++ Bitwise Operators](https://www.programiz.com/cpp-programming/bitwise-operators "C++ Bitwise Operators")
- [C++ Assert](https://www.programiz.com/cpp-programming/assertions "C++ Assert")
- [C++ Buffers](https://www.programiz.com/cpp-programming/buffer "C++ Buffers")
- [C++ istream](https://www.programiz.com/cpp-programming/istream "C++ istream")
- [C++ ostream](https://www.programiz.com/cpp-programming/ostream "C++ ostream")
### C++ Tutorials
- [C++ Priority Queue](https://www.programiz.com/cpp-programming/priority-queue)
- [C++ Stack](https://www.programiz.com/cpp-programming/stack)
- [C++ Deque](https://www.programiz.com/cpp-programming/deque)
- [C++ STL Containers](https://www.programiz.com/cpp-programming/stl-containers)
- [C++ Standard Template Library](https://www.programiz.com/cpp-programming/standard-template-library)
- [C++ Forward List](https://www.programiz.com/cpp-programming/forward-list)
# C++ Queue
In C++, the STL `queue` provides the functionality of a queue data structure.
The queue data structure follows the **FIFO (First In First Out)** principle where elements that are added first will be removed first.

Queue Data Structure
**Recommended Readings**
- [Queue Data Structure](https://www.programiz.com/dsa/queue "Queue Data Structure")
- [C++ STL](https://www.programiz.com/cpp-programming/standard-template-library "C++ STL")
***
## Create C++ STL Queue
In order to create a queue in C++, we first need to include the `queue` header file.
```
#include <queue>
```
Once we import this file, we can create a `queue` using the following syntax:
```
queue<type> q;
```
Here, `type` indicates the [data type](https://www.programiz.com/cpp-programming/data-types "C++ Data Types") we want to store in the queue. For example,
```
// create a queue of integer data type
queue<int> integer_queue;
// create a queue of string data type
queue<string> string_queue;
```
***
## C++ Queue Methods
In C++, `queue` is a [class](https://www.programiz.com/cpp-programming/object-class#class "C++ Class") that provides various methods to perform different operations on a queue.
| Methods | Description |
|---|---|
| `push()` | Inserts an element at the back of the queue. |
| `pop()` | Removes an element from the front of the queue. |
| `front()` | Returns the first element of the queue. |
| `back()` | Returns the last element of the queue. |
| `size()` | Returns the number of elements in the queue. |
| `empty()` | Returns `true` if the queue is empty. |
***
## Insert Element to a Queue
We use the `push()` method to insert an element to the back of a queue. For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string queue<string> animals;
// push elements into the queue animals.push("Cat"); animals.push("Dog");
cout << "Queue: ";
// print elements of queue
// loop until queue is empty
while(!animals.empty()) {
// print the element
cout << animals.front() << ", ";
// pop element from the queue
animals.pop();
}
cout << endl;
return 0;
}
```
**Output**
```
Queue: Cat, Dog,
```
In the above example, we have created a `queue` of [strings](https://www.programiz.com/cpp-programming/strings "C++ Strings") called animals. Here, we have used the `push()` method to add elements to the end of the queue.
```
animals.push("Cat");
animals.push("Dog");
```
Instead of directly printing the contents of the queue, we have used a `while` loop and various queue methods.
```
while(!animals.empty()) {
cout << animals.front() << ", ";
animals.pop();
}
```
This is because the STL queue is an [STL Container Adapter](https://www.programiz.com/cpp-programming/stl-containers#container-adapter) that provides restrictive access to make it behave like a standard queue data structure.
As a result, we cannot iterate through the queue like iterating through [vectors](https://www.programiz.com/cpp-programming/vectors "C++ Vectors") or other [containers](https://www.programiz.com/cpp-programming/stl-containers "STL Containers").
Instead, we print its **front** and then **pop** the element repeatedly inside a loop until the queue is empty.
We will learn about `pop()`, `front()` and `empty()` in the coming sections.
***
## Remove Element from a Queue
We can use the `pop()` method to remove an element from the front of a queue. For example,
```
#include <iostream>
#include <queue>
using namespace std;
// function prototype for display_queue utility
void display_queue(queue<string> q);
int main() {
// create a queue of string
queue<string> animals;
// push element into the queue
animals.push("Cat");
animals.push("Dog");
animals.push("Fox");
cout << "Initial Queue: ";
display_queue(animals);
// remove element from queue animals.pop();
cout << "Final Queue: ";
display_queue(animals);
return 0;
}
// utility function to display queue
void display_queue(queue<string> q) {
while(!q.empty()) {
cout << q.front() << ", ";
q.pop();
}
cout << endl;
}
```
**Output**
```
Initial Queue: Cat, Dog, Fox, Final Queue: Dog, Fox,
```
In the above example, we have used the `pop()` method to remove an element from the queue.
Initially, the contents of the queue are `"Cat"`, `"Dog"` and `"Fox"`.
```
// removes front element
animals.pop();
```
Here `animals.pop()` removes the element at the front of the queue i.e. the element inserted at the very beginning which is `"Cat"`.
Hence, the final queue contains the elements `"Dog"` and `"Fox"`.
***
## Access Element from a Queue
We can access the elements of a `queue` using the following methods:
- `front()` - returns the element from the front of the queue
- `back()` - returns the element from the back of the queue
For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of int
queue<int> nums;
// push element into the queue
nums.push(1);
nums.push(2);
nums.push(3);
// get the element at the front int front = nums.front();
cout << "First element: " << front << endl;
// get the element at the back int back = nums.back();
cout << "Last element: " << back << endl;
return 0;
}
```
**Output**
```
First element: 1 Last element: 3
```
In the above example, we have used the `front()` and `back()` methods to get the first and last elements from a queue of integers called nums.
We can get the first element i.e. the element at the front using:
```
// returns 1
nums.front()
```
Here, **1** was inserted first so it is at the front.
Similarly, we find the last element i.e. the rear (back) element using:
```
// returns 3
nums.back()
```
Here, **3** was inserted last, so it is at the back.
***
## Get the size of a Queue
We use the `size()` method to get the number of elements in the `queue`. For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> languages;
// push element into the queue
languages.push("Python");
languages.push("C++");
languages.push("Java");
// get the size of the queue int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}
```
**Output**
```
Size of the queue: 3
```
In the above example, we have created a queue of strings called languages and added three elements to it.
Then, we used the `size()` method to find the number of elements in the queue:
```
// returns 3
languages.size();
```
Since we have added three elements to the queue, `languages.size()` returns **3**.
***
## Check if the Queue is Empty
We use the `empty()` method to check if the `queue` is empty. This method returns:
- **1 (true)** - if the queue is empty
- **0 (false)** - if the queue is not empty
For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> languages;
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the queue
languages.push("Python");
languages.push("C++");
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
```
**Output**
```
Is the queue empty? Yes Pushing elements... Is the queue empty? No
```
In the above example, we have used the `empty()` method to determine if the `queue` is empty,
```
if (languages.empty()) {
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
```
Initially, the queue has no elements in it. So `languages.empty()` returns `true`.
We then add elements to the queue.
Then we again use `languages.empty()` again to determine if the queue is empty. This time, it returns `false`.
***
**Also Read**
- [C++ Priority Queue](https://www.programiz.com/cpp-programming/priority-queue "C++ Priority Queue")
- [C++ Deque](https://www.programiz.com/cpp-programming/deque "C++ Deque")
- [C++ Stack](https://www.programiz.com/cpp-programming/stack "C++ Stack")
### Table of Contents
- [Introduction](https://www.programiz.com/cpp-programming/queue#introduction)
- [C++ Queue Methods](https://www.programiz.com/cpp-programming/queue#queue-methods)
- [Insert Elements](https://www.programiz.com/cpp-programming/queue#push)
- [Remove Elements](https://www.programiz.com/cpp-programming/queue#pop)
- [Access Elements](https://www.programiz.com/cpp-programming/queue#front-back)
- [Get the size of the Queue](https://www.programiz.com/cpp-programming/queue#size)
- [Check if the Queue is Empty](https://www.programiz.com/cpp-programming/queue#empty)
[Previous Tutorial: C++ Forward List](https://www.programiz.com/cpp-programming/forward-list "C++ Forward List")
[Next Tutorial: C++ Deque](https://www.programiz.com/cpp-programming/deque "C++ Deque")
Share on:
Did you find this article helpful?
Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
[Try Programiz PRO](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_content=interests_learn_coding&utm_term=tutorial_page_footer_banner "Programiz PRO: Premium Learning Platform from Programiz")
- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community
### Related Tutorials
[C++ Tutorial C++ Priority Queue](https://www.programiz.com/cpp-programming/priority-queue)
[C++ Tutorial C++ Stack](https://www.programiz.com/cpp-programming/stack)
[C++ Tutorial C++ Deque](https://www.programiz.com/cpp-programming/deque)
[C++ Tutorial C++ STL Containers](https://www.programiz.com/cpp-programming/stl-containers)
#### Free Tutorials
- [Python 3 Tutorials](https://www.programiz.com/python-programming "Python 3 Tutorials")
- [SQL Tutorials](https://www.programiz.com/sql "SQL Tutorials")
- [R Tutorials](https://www.programiz.com/r "R Tutorials")
- [HTML Tutorials](https://www.programiz.com/html "HTML Tutorials")
- [CSS Tutorials](https://www.programiz.com/css "CSS Tutorials")
- [JavaScript Tutorials](https://www.programiz.com/javascript "JavaScript Tutorials")
- [TypeScript Tutorials](https://www.programiz.com/typescript "TypeScript Tutorials")
- [Java Tutorials](https://www.programiz.com/java-programming "Java Tutorials")
- [C Tutorials](https://www.programiz.com/c-programming "C Tutorials")
- [C++ Tutorials](https://www.programiz.com/cpp-programming "C++ Tutorials")
- [DSA Tutorials](https://www.programiz.com/dsa "Data Structures and Algorithms")
- [C\# Tutorials](https://www.programiz.com/csharp-programming "C# Tutorials")
- [Golang Tutorials](https://www.programiz.com/golang "Golang Tutorials")
- [Kotlin Tutorials](https://www.programiz.com/kotlin-programming "Kotlin Tutorials")
- [Swift Tutorials](https://www.programiz.com/swift-programming "Swift Tutorials")
- [Rust Tutorials](https://www.programiz.com/rust "Rust Tutorials")
- [Ruby Tutorials](https://www.programiz.com/ruby "Ruby Tutorials")
#### Paid Courses
- [Master Python](https://programiz.pro/learn/master-python "Master Python")
- [Learn SQL](https://programiz.pro/course/learn-sql-basics "Learn SQL")
- [Learn HTML](https://programiz.pro/course/learn-html "Learn HTML")
- [Master JavaScript](https://programiz.pro/learn/master-javascript "Master JavaScript")
- [Master C](https://programiz.pro/learn/master-c-programming "Master C")
- [Master C++](https://programiz.pro/learn/master-cpp "Master C++")
- [Master Java](https://programiz.pro/learn/master-java "Master Java")
- [Master DSA with Python](https://programiz.pro/learn/master-dsa-with-python "Master DSA with Python")
#### Online Compilers
- [Python Compiler](https://www.programiz.com/python-programming/online-compiler "Python Compiler")
- [R Compiler](https://www.programiz.com/r/online-compiler "R Compiler")
- [SQL Editor](https://www.programiz.com/sql/online-compiler "SQL Editor")
- [HTML/CSS Editor](https://www.programiz.com/html/online-compiler "HTML/CSS Editor")
- [JavaScript Editor](https://www.programiz.com/javascript/online-compiler "JavaScript Editor")
- [TypeScript Editor](https://www.programiz.com/typescript/online-compiler "TypeScript Editor")
- [Java Compiler](https://www.programiz.com/java-programming/online-compiler "Java Compiler")
- [C Compiler](https://www.programiz.com/c-programming/online-compiler "C Compiler")
- [C++ Compiler](https://www.programiz.com/cpp-programming/online-compiler "C++ Compiler")
- [C\# Compiler](https://www.programiz.com/csharp-programming/online-compiler "C# Compiler")
- [Go Compiler](https://www.programiz.com/golang/online-compiler "Go Compiler")
- [PHP Compiler](https://www.programiz.com/php/online-compiler "PHP Compiler")
- [Swift Compiler](https://www.programiz.com/swift/online-compiler "Swift Compiler")
- [Rust Compiler](https://www.programiz.com/rust/online-compiler "Rust Compiler")
- [Ruby Compiler](https://www.programiz.com/ruby/online-compiler "Ruby Compiler")
#### Mobile Apps
- [Learn Python App](https://www.programiz.com/learn-python "Learn Python: Programiz")
- [Learn C App](https://www.programiz.com/learn-c "Learn C Programming: Programiz")
- [Learn Java App](https://www.programiz.com/learn-java "Learn Java: Programiz")
- [Learn C++ App](https://www.programiz.com/learn-cpp "Learn C++: Programiz")
#### Company
- [Change Ad Consent]()
- [Do not sell my data]()
- [About](https://www.programiz.com/about-us "About us")
- [Contact](https://www.programiz.com/contact "Contact us")
- [Blog](https://www.programiz.com/blog "Blog")
- [Youtube](https://www.youtube.com/channel/UCREFp3D_n8JfcDonlm7Mpyw "Programiz on Youtube")
- [Careers](https://www.programiz.com/careers "Careers")
- [Advertising](https://www.programiz.com/advertise "Advertise with us")
- [Privacy Policy](https://www.programiz.com/privacy-policy "Privacy Policy")
- [Terms & Conditions](https://www.programiz.com/terms-of-use "Terms & Conditions")
© Parewa Labs Pvt. Ltd. All rights reserved. |
| Readable Markdown | In C++, the STL `queue` provides the functionality of a queue data structure.
The queue data structure follows the **FIFO (First In First Out)** principle where elements that are added first will be removed first.

Queue Data Structure
**Recommended Readings**
- [Queue Data Structure](https://www.programiz.com/dsa/queue "Queue Data Structure")
- [C++ STL](https://www.programiz.com/cpp-programming/standard-template-library "C++ STL")
***
## Create C++ STL Queue
In order to create a queue in C++, we first need to include the `queue` header file.
```
#include <queue>
```
Once we import this file, we can create a `queue` using the following syntax:
```
queue<type> q;
```
Here, `type` indicates the [data type](https://www.programiz.com/cpp-programming/data-types "C++ Data Types") we want to store in the queue. For example,
```
// create a queue of integer data type
queue<int> integer_queue;
// create a queue of string data type
queue<string> string_queue;
```
***
## C++ Queue Methods
In C++, `queue` is a [class](https://www.programiz.com/cpp-programming/object-class#class "C++ Class") that provides various methods to perform different operations on a queue.
| Methods | Description |
|---|---|
| `push()` | Inserts an element at the back of the queue. |
| `pop()` | Removes an element from the front of the queue. |
| `front()` | Returns the first element of the queue. |
| `back()` | Returns the last element of the queue. |
| `size()` | Returns the number of elements in the queue. |
| `empty()` | Returns `true` if the queue is empty. |
***
## Insert Element to a Queue
We use the `push()` method to insert an element to the back of a queue. For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string queue<string> animals;
// push elements into the queue animals.push("Cat"); animals.push("Dog");
cout << "Queue: ";
// print elements of queue
// loop until queue is empty
while(!animals.empty()) {
// print the element
cout << animals.front() << ", ";
// pop element from the queue
animals.pop();
}
cout << endl;
return 0;
}
```
**Output**
```
Queue: Cat, Dog,
```
In the above example, we have created a `queue` of [strings](https://www.programiz.com/cpp-programming/strings "C++ Strings") called animals. Here, we have used the `push()` method to add elements to the end of the queue.
```
animals.push("Cat");
animals.push("Dog");
```
Instead of directly printing the contents of the queue, we have used a `while` loop and various queue methods.
```
while(!animals.empty()) {
cout << animals.front() << ", ";
animals.pop();
}
```
This is because the STL queue is an [STL Container Adapter](https://www.programiz.com/cpp-programming/stl-containers#container-adapter) that provides restrictive access to make it behave like a standard queue data structure.
As a result, we cannot iterate through the queue like iterating through [vectors](https://www.programiz.com/cpp-programming/vectors "C++ Vectors") or other [containers](https://www.programiz.com/cpp-programming/stl-containers "STL Containers").
Instead, we print its **front** and then **pop** the element repeatedly inside a loop until the queue is empty.
We will learn about `pop()`, `front()` and `empty()` in the coming sections.
***
## Remove Element from a Queue
We can use the `pop()` method to remove an element from the front of a queue. For example,
```
#include <iostream>
#include <queue>
using namespace std;
// function prototype for display_queue utility
void display_queue(queue<string> q);
int main() {
// create a queue of string
queue<string> animals;
// push element into the queue
animals.push("Cat");
animals.push("Dog");
animals.push("Fox");
cout << "Initial Queue: ";
display_queue(animals);
// remove element from queue animals.pop();
cout << "Final Queue: ";
display_queue(animals);
return 0;
}
// utility function to display queue
void display_queue(queue<string> q) {
while(!q.empty()) {
cout << q.front() << ", ";
q.pop();
}
cout << endl;
}
```
**Output**
```
Initial Queue: Cat, Dog, Fox, Final Queue: Dog, Fox,
```
In the above example, we have used the `pop()` method to remove an element from the queue.
Initially, the contents of the queue are `"Cat"`, `"Dog"` and `"Fox"`.
```
// removes front element
animals.pop();
```
Here `animals.pop()` removes the element at the front of the queue i.e. the element inserted at the very beginning which is `"Cat"`.
Hence, the final queue contains the elements `"Dog"` and `"Fox"`.
***
## Access Element from a Queue
We can access the elements of a `queue` using the following methods:
- `front()` - returns the element from the front of the queue
- `back()` - returns the element from the back of the queue
For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of int
queue<int> nums;
// push element into the queue
nums.push(1);
nums.push(2);
nums.push(3);
// get the element at the front int front = nums.front();
cout << "First element: " << front << endl;
// get the element at the back int back = nums.back();
cout << "Last element: " << back << endl;
return 0;
}
```
**Output**
```
First element: 1 Last element: 3
```
In the above example, we have used the `front()` and `back()` methods to get the first and last elements from a queue of integers called nums.
We can get the first element i.e. the element at the front using:
```
// returns 1
nums.front()
```
Here, **1** was inserted first so it is at the front.
Similarly, we find the last element i.e. the rear (back) element using:
```
// returns 3
nums.back()
```
Here, **3** was inserted last, so it is at the back.
***
## Get the size of a Queue
We use the `size()` method to get the number of elements in the `queue`. For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> languages;
// push element into the queue
languages.push("Python");
languages.push("C++");
languages.push("Java");
// get the size of the queue int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}
```
**Output**
```
Size of the queue: 3
```
In the above example, we have created a queue of strings called languages and added three elements to it.
Then, we used the `size()` method to find the number of elements in the queue:
```
// returns 3
languages.size();
```
Since we have added three elements to the queue, `languages.size()` returns **3**.
***
## Check if the Queue is Empty
We use the `empty()` method to check if the `queue` is empty. This method returns:
- **1 (true)** - if the queue is empty
- **0 (false)** - if the queue is not empty
For example,
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> languages;
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the queue
languages.push("Python");
languages.push("C++");
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
```
**Output**
```
Is the queue empty? Yes Pushing elements... Is the queue empty? No
```
In the above example, we have used the `empty()` method to determine if the `queue` is empty,
```
if (languages.empty()) {
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
```
Initially, the queue has no elements in it. So `languages.empty()` returns `true`.
We then add elements to the queue.
Then we again use `languages.empty()` again to determine if the queue is empty. This time, it returns `false`.
***
**Also Read**
- [C++ Priority Queue](https://www.programiz.com/cpp-programming/priority-queue "C++ Priority Queue")
- [C++ Deque](https://www.programiz.com/cpp-programming/deque "C++ Deque")
- [C++ Stack](https://www.programiz.com/cpp-programming/stack "C++ Stack") |
| Shard | 35 (laksa) |
| Root Hash | 16786490165506891835 |
| Unparsed URL | com,programiz!www,/cpp-programming/queue s443 |