![]() |
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++:
Output Original Deque: 10 20 30 40 50 Updated Deque: 5 10 20 30 40 50 Time Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |