In: Computer Science
Indicate six key differences between a deque and a vector. in C++
Major differences between deque and vector are as follows:
1. Major difference is that Vector provides insertion and deletion at middle and end only. Whereas, deque provides methods/operations for insertion at front, middle and end.
2. Basically,Vector provides good performance while insertion and deletion at end only and bad performance for insertion and deletion at middle. But Deque provides same kind of performance as vector for insertion and deletion at end and middle. But deque also provides good performance for insertion and deletion at front.
3. Vector stores elements contiguously, where as deque internally contains a list of memory chunks which store elements in contiguous order.
4. Generally,Performance of addition and deletion at end for vector is better than deque.
5. No Iterator invalidation happens in deque for insertion and deletion at front and end because like vectors, deque doesn’t have to shift elements from one memory to another in case current allocated memory is not sufficient to store the newly added element.
6. Performance of random access in deque will be little slower than vector.