The difference between vector and deque

rajenpandit picture rajenpandit · Feb 27, 2014 · Viewed 28.6k times · Source

As vector and deque both provides a function to push_back the element at the last.

where deque also provides a function push_front to insert the element at the beginning, which is bit costly in case of vector.

My question is when we can achieve the same functionality (push_back) of vector by using deque, then why vector is required?

Answer

paxdiablo picture paxdiablo · Feb 27, 2014

One main difference between vectors and deques is that the latter allows efficient insertion at the front of the structure as well as the back.

Deques also do not guarantee that their elements are contiguous in memory so the at-style operator (indexing) may not be as efficient.

Note that the difference is unlikely to matter in practice for smaller collections but would generally become more important if, for example, the collection size increases or you're modifying it many times per second.