How to Iterate a STL Queue in C++?
Last Updated :
06 Feb, 2023
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 does not provide std: :begin function and std: :end function from which we can iterate on the queue using iterator. In simple words std: :queue is not iterated over.
There are 3 methods from which we can iterate on queue i.e.
- Using standard std: :front and std: :pop method
- Creating copy of given std: :queue
- Using std: :deque
1. Using standard std : : front and std : : pop method
We can iterate on queue using std: :front which will return the front element of the queue and std: :pop which will remove the front element of the queue. But, after iterating on the queue with this method queue will vanish as we are deleting elements of the queue as we iterate over it.
Example:
C++
// C++ program to iterate a STL Queue
// using standard std : : front and
// std : : pop method
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// creating std :: queue in c++
queue<int> q;
// inserting elements in queue
// using std :: push method
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
cout << "Elements of queue are : \n";
while (!q.empty()) {
// getting front element of queue
cout << q.front() << " ";
// removing front element of queue
q.pop();
}
return 0;
}
OutputElements of queue are :
1 2 3 4 5
2. Creating copy of given std : : queue
If we want to iterate on std: :queue then we can create a temporary copyqueue and we can copy all elements of queue into copyqueue then we can easily traverse on copyqueue and find the elements of queue using std: :front() function and delete element from queue using std: :pop function.
Example:
C++
// C++ program to iterate a STL Queue
// by Creating copy of given
// std : : queue
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// creating std :: queue in c++
queue<int> q;
// pushing elements using std :: push()
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
// creating copy queue to
// copy all elements of queue
queue<int> copy_queue = q;
cout << "Elements of queue are :\n";
// traversing on copyqueue
// until it becomes empty
while (!copy_queue.empty()) {
// printing front element of queue
cout << copy_queue.front() << " ";
// deleting element from
// queue using std :: pop()
// function
copy_queue.pop();
}
return 0;
}
OutputElements of queue are :
1 2 3 4 5
3. Using std : : deque
In above method extra copy queue is required to first copy all elements of queue which is not space efficient method. By using std : : dequeue we can iterate with efficient space which provides all the standard operations of std: :queue. We can traverse on dequeue using for range based for loop.
We can use dequeue: :cbegin() and dequeue: :cend() to print dequeue in forward direction and dequeue: :crbegin() and dequeue: :crend() to print dequeue in backward direction.
Example:
C++
// C++ program to iterate a STL Queue
// using std : : dequeue
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
deque<int> dq;
dq.push_back(1);
dq.push_back(2);
dq.push_back(3);
dq.push_back(4);
dq.push_back(5);
cout << "Printing dequeue in forward direction : \n";
for(auto it = dq.cbegin();it!=dq.cend();it++)
{
cout << *it << " ";
}
cout << "\n";
cout << "Printing dequeue in reverse direction : \n";
for(auto it = dq.crbegin();it!=dq.crend();it++)
{
cout << *it << " ";
}
return 0;
}
OutputPrinting dequeue in forward direction :
1 2 3 4 5
Printing dequeue in reverse direction :
5 4 3 2 1
Time complexity: O(n) // n is the size of the queue.
Auxiliary space: O(n).
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
4 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 arti
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 ContentBy Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Making
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