Queue in Standard Template Library (STL)

Queues 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.
 The functions supported by queue are : 

  1. empty() – Returns whether the queue is empty.
  2. size() – Returns the size of the queue.
  3. queue::swap() in C++ STL: Exchange the contents of two queues but the queues must be of same type, although sizes may differ.
  4. queue::emplace() in C++ STL: Insert a new element into the queue container, the new element is added to the end of the queue.
  5. queue::front() and queue::back() in C++ STLfront() function returns a reference to the first element of the queue. back() function returns a reference to the last element of the queue.
  6. push(g) and pop()push() function adds the element ‘g’ at the end of the queue. pop() function deletes the first element of the queue.






// CPP code to illustrate
// Queue in Standard Template Library (STL)
#include <iostream>
#include <queue>
using namespace std;
// Print the queue
void showq(queue<int> gq)
    queue<int> g = gq;
    while (!g.empty()) {
        cout << '\t' << g.front();
    cout << '\n';
// Driver Code
int main()
    queue<int> gquiz;
    cout << "The queue gquiz is : ";
    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.front() : " << gquiz.front();
    cout << "\ngquiz.back() : " << gquiz.back();
    cout << "\ngquiz.pop() : ";
    // We can also use front and back as
    // iterators to traverse through the queue
    cout << "Using iterators : ";
    for (auto i = gquiz.front(); i != gquiz.back(); i++) {
        cout << i << " ";
    return 0;



The queue gquiz is :     10    20    30

gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() :     20    30
Using iterators : 20 21 22 23 24 25 26 27 28 29 

Recent Articles on C++ Queue 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.