How to Add Element at Front of a Deque in C++? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a deque is a vector like data structure that allows users to add and remove elements from both the front and the back ends. In this article, we will learn how to add an element at the beginning of a deque in C++. Example: Input: myDeque: {1, 2, 3} Output: myDeque = {11, 1, 2, 3}Add an Element at the Beginning of a Deque in C++To add an element at the beginning of a std::deque in C++, we can use the deque::push_front () method. This method takes the value to be inserted as an argument and inserts it to the beginning of the deque. Syntax:dq_name.push_front(value);C++ Program to Add an Element at the Beginning of a DequeThe following program illustrates how we can add an element at the beginning of a deque in C++: C++ // C++ Program to illustrates how we can add an element at // the beginning of a deque #include <deque> #include <iostream> using namespace std; int main() { // Initializing a deque deque<int> dq = { 10, 20, 30, 40, 50 }; // Print the Original Deque cout << "Original Deque: "; for (int ele : dq) { cout << ele << " "; } cout << endl; // Element to add at the beginning int element = 5; // Adding element at the beginning of the deque dq.push_front(element); // Print the Updated Deque cout << "Updated Deque: "; for (int ele : dq) { cout << ele << " "; } return 0; } OutputOriginal Deque: 10 20 30 40 50 Updated Deque: 5 10 20 30 40 50 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Add Element at Front of a Deque in C++? D dasrudra0710 Follow Improve Article Tags : C++ Programs C++ cpp-deque CPP Examples Practice Tags : CPP Similar Reads How to Add an Element at the End of a Deque in C++? In C++, a deque is a flexible data structure that supports the insertion and deletion of elements from both ends efficiently. In this article, we will learn how to add an element at the end of a deque in C++. Example: Input: myDeque ={10,20,30,40} Output: // added element 50 at the end myDeque ={10, 2 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Access the First Element of a Deque in C++? In C++, deques also called double-ended queues are sequence containers that can perform insertions and deletions on both ends. In this article, we will learn how to access the first element of a deque in C++. Example: Input: myDeque = {1,2,3,4,5} Output: Deque Elements: 1 2 3 4 5 First Element: 1Acc 2 min read How to Add an Element at the Front of a List in C++? In C++, the STL has a doubly linked list container defined as the std::list class template inside the <list> header. It stores the sequential data in non-contiguous memory locations. In this article, we will learn how to add an element at the beginning of a list in C++ STL. Example: Input: myL 2 min read How to Clear All Elements from a Deque in C++? In C++, deque also known as double-ended queue is a container provided by the Standard Template Library (STL) that allows efficient insertion and deletion at both ends. In this article, we will learn how to clear all elements from a deque in C++. Example: Input: myDeque = {10, 20, 30, 40}; Output: A 2 min read Like