Search code examples
c++vectorstldeque

What is the reasoning for a deque to have a subscript operator?


I was talking to a friend today and he brought up how weird it is that deque elements are able to be accessed by subscript operator when other "similar" DSs, like queue and stack are not. After all, doesn't deque just mean double-ended queue? Doesn't the fact that deque's can be randomly accessed ruin the very integrity of the deque data structure?

Also, for a performance standpoint, don't deque's just bring less to the table entirely with the subscript operator unless you're actually utilizing deque methods (the family of front and back functions)? I understand that the implementation behind deque's splits it up into chunks/blocks, and that it generally takes two operations to actually randomly access a element, whereas vectors are guaranteed to be in contiguous memory.

Thanks!


Solution

  • Doesn't the fact that deque's can be randomly accessed ruin the very integrity of the deque data structure?

    No. You are just hung up on the name for it. We do have a clear definition of a "queue" in mind, so this is a poor choice of words perhaps, but a data structure is not defined by its name.

    A date structure is data (duh) with a specific set of associated operations. The standard library defined deque with a subscript operator. So that's the data structure. Great care was taken to make sure the various requirements on the operations don't conflict, so the integrity of deque as a data structure is held up quite nicely.