We know that the standard container class templates deque and list can be used to implement queue and +vector to implement stack. But, what are difference between these two implementation, if we always use the same methods to access and also can not access arbitrary elements using at() or [] as we do it with deque (vector).
1 Answer
The container adaptors, such as stack and queue, are implemented using a specific subset of operations - any container that will be used with a particular adaptor must support all of the operations that the adaptor requires.
vector cannot be used with the queue container adaptor because:
Any sequence supporting operations front(), back(), push_back() and pop_front() can be used to instantiate queue.
And vector doesn't support pop_front().
vector, deque or list can be used with stack because all three of these containers support the operations that stack requires:
Any sequence supporting operations back(), push_back() and pop_back() can be used to instantiate stack.
3 Comments
vector does not support pop_front because there it cannot (really) be implemented efficiently.