Queue in STL C++

About Queue in STL :

Queue in STL is defined as the container used first in first out (FIFO) principle to inserted elements at the back end and deleted from the front.

Queue in stl

Syntax of Queue :

queue< data_type> queue_name;

In the STL, queues are dynamic storage containers that are implemented as memory-resident queue data structures. They operate according to the FIFO (first in first out) principle, which states that the last element will be the final one to be removed. In a queue, elements are kept together in a continuous way. The head of the queue is used for insertion, while the back of the queue is always used for deletion.

Functions in Queue :

 FunctionWorking
push()insert an element at back end of the queue.
front()give the first element of the queue.
size()give the number of elements present in the queue
empty()tells whether the queue is empty or not.
pop()remove the elements from the front of queue.

Example of  Queue in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
    queue qq;
    qq.push(1);
    qq.push(2);
    qq.push(3);
    qq.push(5);
    
    //diplaying queue elements
    cout<<"The elements in the queue : ";
    while(!qq.empty()){
        cout<< qq.front()<<" ";
        qq.pop();
    }

    return 0;
}

Output :

The elements in the queue : 1 2 3 5

In the above program, we take the queue qq, push the elements 1,2,3,5 in it then print the  elements of queue.

Example of Queue in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
    queue qq;
    qq.push(1);
    qq.push(2);
    qq.push(3);
    qq.push(5);
    cout<<"The size of queue : "<< qq.size()<< endl;
    
    // remove element from front of the queue
    qq.pop();
    
    //diplaying queue elements
    cout<<"The elements in the queue : ";
    while(!qq.empty()){
        cout<< qq.front()<<" ";
        qq.pop();
    }
    cout<< endl;
    
    cout<<"The size of queue : "<< qq.size()<< endl;
    if(qq.empty()){
        cout<<"Queue is empty";
    }
    else{
        cout<<"Queue is not empty";
    }

    return 0;
}

Output :

The size of queue : 4
The elements in the queue : 2 3 5 
The size of queue : 0
Queue is empty

In the above program, we take the queue qq, push the elements 1,2,3,5 in it and by using pop()  function remove the front element of the queue then print the size and elements of queue.

Queue is empty because while printing queue elements we use qq.pop() which will remove the elements from the queue.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription