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

06 Oct, 2021
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. 
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  
No-Throw-Guarantee – if an exception is thrown, there are no changes in the containerIf the deque is empty, it shows undefined behavior.
No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
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
 
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  
No-Throw-Guarantee – if an exception is thrown, there are no changes in the containerIf the deque is empty, it shows undefined behaviour.
No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
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
 
hritikbhatnagar2182
cpp-containers-library
cpp-deque
CPP-Library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Inheritance in C++
C++ Classes and Objects
Operator Overloading in C++
Socket Programming in C/C++
Bitwise Operators in C/C++
Virtual Function in C++
Constructors in C++
Templates in C++ with Examples
Object Oriented Programming in C++
Copy Constructor in C++