Removing the First Element from a Deque in C++
To remove an element from the beginning of a deque in C++, we can use the std::deque::pop_front() member function of std::deque that dequeues the first element, effectively removing it from the deque.
Syntax to Use std::deque::pop_front
dequeName.pop_front();
C++ Program to Remove an Element from the Beginning of a Deque
The below program demonstrates how we can remove the first element from the beginning of a deque in C++.
// C++ Program to demonstrates how we can remove the first
// element from the beginning of a deque
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Creating a deque
deque<int> myDeque = { 1, 2, 3, 4, 5 };
// Printing the original deque
cout << "Original Deque: ";
for (int num : myDeque) {
cout << num << " ";
}
cout << endl;
// Removing the element from the beginning of the deque
myDeque.pop_front();
// Printing the updated deque
cout << "Deque After Removal: ";
for (int num : myDeque) {
cout << num << " ";
}
cout << endl;
return 0;
}
Output
Original Deque: 1 2 3 4 5 Deque After Removal: 2 3 4 5
Time Complexity: O(1)
Auxiliary Space: O(1)
How to Remove an Element from Front of Deque in C++?
In C++, a deque (double-ended queue) is a data structure that allows efficient insertion and deletion at both ends. In this article, we will learn to remove an element from the beginning of a deque in C++.
Example:
Input: myDeque = {1, 2, 3, 4, 5}; Output: Deque After Removal: 2 3 4 5