STL deque accessing by index is O(1)?

Posted by jasonline on Stack Overflow See other posts from Stack Overflow or by jasonline
Published on 2010-02-19T14:54:27Z Indexed on 2011/01/14 17:53 UTC
Read the original article Hit count: 202

Filed under:
|
|
|

I've read that accessing elements by position index can be done in constant time in a STL deque. As far as I know, elements in a deque may be stored in several non-contiguous locations, eliminating safe access through pointer arithmetic. For example:

abc->defghi->jkl->mnop

The elements of the deque above consists of a single character. The set of characters in one group indicate it is allocated in contiguous memory (e.g. abc is in a single block of memory, defhi is located in another block of memory, etc.). Can anyone explain how accessing by position index can be done in constant time, especially if the element to be accessed is in the second block? Or does a deque have a pointer to the group of blocks?

Update: Or is there any other common implementation for a deque?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl