Skip to content
Related Articles

Related Articles

How to Iterate a STL Queue in C++?

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 07 Sep, 2022
Improve Article
Save Article

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.

  1. Using standard std: :front and std: :pop method
  2. Creating copy of given std: :queue
  3. 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;
}


Output

Elements 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;
}


Output

Elements 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;
}


Output

Printing dequeue in forward direction : 
1 2 3 4 5 
Printing dequeue in reverse direction : 
5 4 3 2 1 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!