Skip to content
Related Articles

Related Articles

Deque vs Vector in C++ STL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 10 Jul, 2022
View Discussion
Improve Article
Save Article

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 guaranteed. 
Double Ended Queues are basically an implementation of the data structure double-ended queue. A queue data structure allows insertion only at the end and deletion from the front. This is like a queue in real life, wherein people are removed from the front and added at the back. Double-ended queues are a special case of queues where insertion and deletion operations are possible at both ends. 

The functions for deque are the same as a vector, with the addition of push and pop operations for both front and back. 

Vector in C++ STL

Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time. 

Difference between Deque and Vector:

VectorDeque
Provides insertion and deletion methods at middle and endProvides insertion and deletion methods at middle, end, beginning
Bad performance for insertion and deletion at the frontGood performance for insertion and deletion at the front
Stores elements contiguouslyIt contains lists of memory chunks where elements are stored contiguously
Good performance for addition and deletion of elements at the endGood performance for addition and deletion of elements at the end
It is stored in <vector> header file in C++It is stored in <deque> header file in C++
Its time complexity of insertion is O(N)Its time complexity of insertion is O(1)
Its time complexity of deletion is O(N)Its time complexity of deletion is O(N)

When should we choose Deque over Vector? 
We must choose Deque when our operations are adding and deleting elements in the beginning and at the end (Double-ended queue ADT).
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!