How Deque Works Internally in C++? Last Updated : 21 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Prerequisite: Deque in C++ Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1) time complexity. Practical Applications of Deque Deque's ability to insert and delete from both sides makes it one of the most useful containers in STL. Real-world applications are mentioned below: Applied as both stack and queue, as it supports both operations.Storing a web browser’s history.Storing a software application’s list of undo operations.Job scheduling algorithmHow Deque Works Internally in C++ STL An STL deque is implemented using arrays of data or arrays of pointers to memory blocks rather than a linked list. Depending on storage requirements, the number of blocks and size of the array of pointers fluctuate dynamically. These memory blocks contain items at adjacent locations. A block of memory is automatically allocated when a deque object is created so that the objects can be stored in contiguous locations. Deque then allocates a new block of memory and joins the front of the prior memory block with it when we put an item in front of it. Now, if we add pieces to the front once more, they will be stored in this new memory block until it is entirely full.When an item is inserted at the end of a deque, the allocated block of memory holds it until it is completely filled; if this occurs, a new block of memory is allocated and connected to the end of the preceding block. Elements that are added to the deque's back are now stored in that new memory block. Since a deque does not need shifting an element by 1 as an array does in a block of memory to make room at the front, you can think of it as a linked list of vectors. In order to allocate a new block of memory to it, it first determines if there is space remaining at the front of the first element in the current block of memory. Time Complexity:Accessing Elements- O(1)Insertion or removal of elements- O(N)Insertion or removal of elements at start or end- O(1) Example: C++ // C++ code to show working of the deque #include <deque> #include <iostream> using namespace std; // Driver Code int main() { deque<int> d = { 1, 2, 3 }; d.push_back(4); d.push_front(0); cout << "Elements in Deque: " << endl; for (int i : d) cout << i << " "; cout << endl; d.pop_back(); cout << "\n" << "Elements in Deque after pop_back(): " << endl; for (int i : d) cout << i << " "; cout << endl; cout << "\n" << "Elements in Deque after pop_front(): " << endl; d.pop_front(); for (int i : d) cout << i << " "; cout << endl; cout << "\n" << "Element in front of deque: " << d.front() << endl; cout << "Element in back of deque: " << d.back() << endl; cout << "Item at 1th Index: " << d.at(1) << endl; cout << "Size of deque: " << d.size() << endl; cout << "Is deque empty: " << d.empty() << endl; cout << "\n" << "After deleting all elements of deque:" << "\n\n"; d.erase(d.begin(), d.end()); cout << "Size of deque: " << d.size() << endl; cout << "Is deque empty: " << d.empty() << endl; } OutputElements in Deque: 0 1 2 3 4 Elements in Deque after pop_back(): 0 1 2 3 Elements in Deque after pop_front(): 1 2 3 Element in front of deque: 1 Element in back of deque: 3 Item at 1th Index: 2 Size of deque: 3 Is deque empty: 0 After deleting all elements of deque: Size of deque: 0 Is deque empty: 1 Comment More infoAdvertise with us Next Article Deque of Pairs in C++ with Examples akashjha2671 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL cpp-deque +1 More Practice Tags : CPPSTL Similar Reads Deque in C++ STL In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.Example:C++#include <iostream> #include <deque> using 6 min read Commonly Used Methodsdeque::push_front() in C++ STLDeque 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 2 min read deque::push_back() in C++ STLDeque 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 2 min read deque::pop_front() and deque::pop_back() in C++ STLDeque 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 4 min read Deque::front() and deque::back() in C++ STLDeque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may 4 min read deque insert() function in C++ STLThe deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by in 3 min read deque::begin() and deque::end in C++ STLDeque 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 3 min read Deque::empty() and deque::size() in C++ STLDeque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may 4 min read deque::clear() and deque::erase() in C++ STLDeque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation 5 min read Other Member Methodsdeque max_size() function in C++ STLThe deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque c 1 min read deque assign() function in C++ STLThe deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. Syntax: de 2 min read deque rbegin() function in C++ STLThe deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which po 2 min read deque rend() function in C++ STLThe deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reve 2 min read deque cbegin() in C++ STLThe cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t 2 min read deque cbegin() in C++ STLThe cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t 2 min read deque::operator= and deque::operator[] in C++ STLDeque 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 4 min read deque::at() and deque::swap() in C++ STLDeque 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 4 min read How Deque Works Internally in C++? Prerequisite: Deque in C++ Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1) 4 min read Deque of Pairs in C++ with Examples What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s 5 min read Difference between Queue and Deque in C++ Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty 4 min read Deque vs Vector in C++ STL Deque in C++ Standard Template Library (STL) Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors but support inserting and deleting the first element in O(1). Unlike vectors, contiguous storage allocation is not guarante 2 min read How to check/find an item in Dequeue using find() method find() function finds the element in the given range of numbers. Returns an iterator to the first element in the range [first, last) that compares equal to the value to be searched. If no such element is found, the function returns last. Syntax: InputIterator find (InputIterator first, InputIterator 6 min read Like