Deque in STL C++

About Deque in STL :

Deque in STL is defined as the container used to insert and remove elements at both ends. They are implementing using queue data structure.

deque in stl

Syntax of Deque :

deque< data_type> deque_name;

In STL, Sequence containers having the ability to expand and shrink on both ends are known as double-ended queues. They are comparable to vectors but more effective when adding and removing components. Contiguous storage allocation might not be assured, unlike vectors. Dequeues are essentially an implementation of the double-ended queue data structure. A queue data structure only permits deletion from the front and only allows insertion from the end.This is similar to a line in real life when individuals are added at the rear and removed from the front.
Dequeues are a particular kind of queue where both ends can perform insertion and deletion operations. With the inclusion of push and pop operations for the front and rear, deque functions are identical to vector functions.

Functions in Deque :

 FunctionWorking
push_back()insert an element to the back end of the deque.
push_front()insert an element to the front end of the deque.
size()give the number of elements present in the deque.
empty()tells whether the deque is empty or not.
max_size()

return the maximum number of elements can deque hold.

Example of Deque in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
   deque< int> dq;
   dq.push_back(10);
   dq.push_front(20);
   dq.push_front(30);
   dq.push_back(40);
   dq.push_front(50);
   cout<<"The elements in the deque :";
   for(auto it : dq){
       cout<< it << " ";
   }
   cout<< endl;
   cout<< "The size of deque is : "<< dq.size();

    return 0;
}

Output :

The elements in the deque :50 30 20 10 40 
The size of deque is : 5

In the above program, we take the deque , push 50, 30,20,10,40 in it and print them on the screen.

Example of Deque in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
   deque< int> dq;
   dq.push_back(10);
   dq.push_front(20);
   dq.push_front(30);
   dq.push_back(40);
   dq.push_front(50);
   cout<<"The elements in the deque :";
   for(auto it : dq){
       cout<< it << " ";
   }
   cout<< endl;
   cout<<"The size of deque is : "<< dq.size()<< endl;
   // maximum elements deque can hold
   cout<<"The max_size of deque : "<<  dq.max_size()<< endl;
   dq.clear();
   if(dq.empty()){
        cout<<"Deque is empty";
   }
   else{
       cout<<"Deque is not empty";
   }

    return 0;
}

Output :

The elements in the deque :50 30 20 10 40 
The size of deque is : 5
The max_size of deque : 2305843009213693951
Deque is empty

In the above program, we take the deque , push 50, 30,20,10,40 in it and apply different functions like clear(), max_size(), empty() , size() and see the various result.

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