Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL deque accessing by index is O(1)?

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?

like image 261
jasonline Avatar asked Feb 19 '10 14:02

jasonline


People also ask

Does deque have an index?

std::deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end. In addition, insertion and deletion at either end of a deque never invalidates pointers or references to the rest of the elements.

Can we Index deque?

If deque is implemented as a ring buffer on top of std::vector , which reallocates itself when it grows in size, then accessing by index is indeed O(1). when erasing elements from the deque, it may call as many assignment operators, as the distance from the elements being erased to the end of the deque is.

What is deque in STL?

In C++, the STL deque is a sequential container that provides the functionality of a double-ended queue data structure. In a regular queue, elements are added from the rear and removed from the front. However, in a deque, we can insert and remove elements from both the front and rear. Deque Data Structure.

How is deque in STL implemented?

deque could be implemented as a circular buffer of fixed size array: Use circular buffer so we could grow/shrink at both end by adding/removing a fixed sized array with O(1) complexity. Use fixed sized array so it is easy to calculate the index, hence access via index with two pointer dereferences - also O(1)

Is it possible to access elements in STL deque by Index?

STL deque accessing by index is O (1)? Bookmark this question. Show activity on this post. 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:

Is deque really O (1)?

If deque is implemented as a ring buffer on top of std::vector, which reallocates itself when it grows in size, then accessing by index is indeed O (1). The standard provides evidence that such implementation was meant--at least it conforms to standard in complexity estimations.

What is the difference between deque and deque Rend in C++ STL?

deque rend() function in C++ STL: Returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). deque cbegin() in C++ STL: Returns a constant iterator pointing to the first element of the container, that is, the iterator cannot be used to modify,...

What is the difference between deque insert and deque rbegin?

deque insert () function in C++ STL: Inserts an element. And returns an iterator that points to the first of the newly inserted elements. deque rbegin () function in C++ STL: Returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning).


1 Answers

I found this deque implementation from Wikipedia:

Storing contents in multiple smaller arrays, allocating additional arrays at the beginning or end as needed. Indexing is implemented by keeping a dynamic array containing pointers to each of the smaller arrays.

I guess it answers my question.

like image 52
jasonline Avatar answered Sep 17 '22 19:09

jasonline