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


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 behaviour.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

8 7 6 5 4 3 2 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

82 73 64 57 43 39 20 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.