How to Access the First Element of a Deque in C++? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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: 1Accessing the First Element of a Deque in C++ To access the first element of a std::deque in C++, we can use the std::deque::front() method which returns a reference to the first element of the deque. This function is suitable for accessing and manipulating the first element without modifying the deque's structure. Syntaxdeque_name.front(); Here: deque_name: Denotess the deque container.front(): This denotes the function used to access the first element of the deque.C++ Program to Access the First Element of a DequeThe below program demonstrates how we can access the first element of a deque in C++. C++ // C++ Program to illustrates how to access the first // element of a deque #include <deque> #include <iostream> using namespace std; int main() { // Initializing a deque deque<int> dq = { 1, 2, 3, 4, 5 }; // Printing deque elements cout << "Deque Elements:"; for (int num : dq) { cout << " " << num; } cout << endl; // Accessing the first element using front() int frontElement = dq.front(); // Printing the first element cout << "First Element: " << frontElement << endl; return 0; } OutputDeque Elements: 1 2 3 4 5 First Element: 1 Time Complexity: O(1)Auxilliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access the First Element of a Deque in C++? G gpancomputer Follow Improve Article Tags : C++ Programs C++ cpp-deque CPP Examples Practice Tags : CPP Similar Reads How to Access the Last Element of a Deque in C++? In C++ STL, we have a deque container which is a double-ended queue that allows us to add or remove elements from both ends. In this article, we will learn how to access the last element in a deque in C++. For Example, Input:myDeque = {1, 2, 3, 4, 5, 6}Output: Last Element: 6Accessing the Last Eleme 2 min read How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int 2 min read How to Access First Element of a List in C++? In C++, a list is a sequential container that allows constant time insertions and deletions and allows the users to store data in non-contiguous memory locations. In this article, we will learn how to access the first element in the list in C++. Examples: Input: myList ={1, 2, 3, 4, 5} Output: The f 2 min read How to Add Element at Front of a Deque in C++? 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 Elemen 2 min read How to Find the Sum of All Elements in a Deque in C++? In C++, double-ended queues are sequence containers similar to vectors but are more efficient in the case of insertion and deletion of elements at both ends. In this article, we will learn how to find the sum of all elements in a deque in C++. Example Input: myDeque ={1,2,3,4,5} Output: Sum of all d 2 min read Like