Skip to content
Related Articles

Related Articles

deque::pop_front() and deque::pop_back() in C++ STL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 23 Jun, 2022
View Discussion
Improve Article
Save Article

Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.
 

deque::pop_front()

pop_front() function is used to pop or remove elements from a deque from the front. The value is removed from the deque from the beginning, and the container size is decreased by 1.
Syntax : 
 

dequename.pop_front()
Parameters :
No value is needed to pass as the parameter.
Result :
Removes the value present at the front 
of the given deque named as dequename

Examples: 
 

Input  :  mydeque = 1, 2, 3
          mydeque.pop_front();
Output :  2, 3

Input  :  mydeque = 3, 4, 1, 7, 3
          mydeque.pop_front();
Output :  4, 1, 7, 3

Errors and Exceptions 
 

  1. No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
  2. If the deque is empty, it shows undefined behavior.

CPP




// CPP program to illustrate
// pop_front() function
#include <iostream>
#include <deque>
using namespace std;
  
int main()
{
    deque<int> mydeque;
    mydeque.push_front(3);
    mydeque.push_front(2);
    mydeque.push_front(1);
    //Deque becomes 1, 2, 3
 
    mydeque.pop_front();
    //Deque becomes 2, 3
  
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
}

Output: 
 

2 3

Application: Input an empty deque with the following numbers and order using push_front() function and print the reverse of the deque. 
 

Input : 1, 2, 3, 4, 5, 6, 7, 8
Output: 8, 7, 6, 5, 4, 3, 2, 1

 

CPP




// CPP program to illustrate
// application Of pop_front() function
#include <iostream>
#include <deque>
using namespace std;
  
int main()
{
    deque<int> mydeque{}, newdeque{};
    mydeque.push_front(8);
    mydeque.push_front(7);
    mydeque.push_front(6);
    mydeque.push_front(5);
    mydeque.push_front(4);
    mydeque.push_front(3);
    mydeque.push_front(2);
    mydeque.push_front(1);
  
    //Deque becomes 1, 2, 3, 4, 5, 6, 7, 8
  
    while (!mydeque.empty()) {
        newdeque.push_front(mydeque.front());
        mydeque.pop_front();
    }
    for (auto it = newdeque.begin(); it != newdeque.end(); ++it)
        cout << ' ' << *it;
}

Output: 
 

8 7 6 5 4 3 2 1

Time complexity: O(1)

Auxiliary Space: O(1)

deque::pop_back()

pop_back() function is used to pop or remove elements from a deque from the back. The value is removed from the deque from the end, and the container size is decreased by 1.
Syntax : 
 

dequename.pop_back()
Parameters :
No value is needed to pass as the parameter.
Result :
Removes the value present at the end or back 
of the given deque named as dequename

Examples: 
 

Input  :  mydeque = 1, 2, 3
          mydeque.pop_back();
Output :  1, 2

Input  :  mydeque = 3, 4, 1, 7, 3
          mydeque.pop_back();
Output :  3, 4, 1, 7

Errors and Exceptions 
 

  1. No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
  2. If the deque is empty, it shows undefined behaviour.

CPP




// CPP program to illustrate
// pop_back() function
#include <iostream>
#include <deque>
using namespace std;
  
int main()
{
    deque<int> mydeque;
    mydeque.push_front(5);
    mydeque.push_front(4);
    mydeque.push_front(3);
    mydeque.push_front(2);
    mydeque.push_front(1);
    //Deque becomes 1, 2, 3, 4, 5
 
    mydeque.pop_back();
    //Deque becomes 1, 2, 3, 4
  
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
}

Output: 
 

1 2 3 4

Application : 
Input an empty deque with the following numbers and order using push_front() function and print the reverse of the deque. 
 

Input  : 1, 20, 39, 43, 57, 64, 73, 82
Output : 82, 73, 64, 57, 43, 39, 20, 1

CPP




// CPP program to illustrate
// application Of pop_back() function
#include <iostream>
#include <deque>
using namespace std;
  
int main()
{
    deque<int> mydeque, newdeque;
    mydeque.push_front(82);
    mydeque.push_front(73);
    mydeque.push_front(64);
    mydeque.push_front(57);
    mydeque.push_front(43);
    mydeque.push_front(39);
    mydeque.push_front(20);
    mydeque.push_front(1);
  
    //Deque becomes 1, 20, 39, 43, 57, 64, 73, 82
  
    while (!mydeque.empty()) {
        newdeque.push_back(mydeque.back());
        mydeque.pop_back();
    }
    for (auto it = newdeque.begin(); it != newdeque.end(); ++it)
        cout << ' ' << *it;
}

Output: 
 

82 73 64 57 43 39 20 1

 Time complexity: O(1)

Auxiliary Space: O(1)

Let us see the differences in a tabular form -:

 deque::pop_front() deque::pop_back() 
1.It is used to remove the first element in the deque containerIt is used to remove the last element in the deque container
2.

Its syntax is -:

pop_front();

Its syntax is -:

pop_back();

3.It does not take any parameters.It does not take any parameters.
4.It does not have any return value.It does not have any return value.
5.Its complexity is constant.Its complexity is constant.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!