STL deque accessing by index is O(1)?

前端 未结 4 2034
眼角桃花
眼角桃花 2020-12-05 09:13

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-contiguo

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 10:04

    One of the possible implementation can be a dynamic array of const-sized arrays; such a const-sized array can be added to either end when more space is needed. All the arrays are full except, maybe, for the first and the last ones which can be partly empty. That means knowing the size of each array and the number of the elements used in the first array one can easily find a position of an element by index.
    http://cpp-tip-of-the-day.blogspot.ru/2013/11/how-is-stddeque-implemented.html

提交回复
热议问题