A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.
Questions tagged [deque]
993 questions
12
votes
2 answers
How are random access iterators for non-contiguous containers (such as std::deque) implemented?
I understand how random access iterators work for contiguous containers like std::vector: the iterator simply maintains a pointer to the current element and any additions/subtractions are applied to the pointer.
However, I'm baffled as to how…

chbaker0
- 1,758
- 2
- 13
- 27
12
votes
1 answer
Where is the "*simpler* real-time catenable deque" work of Tarjan and Mihaescu?
I've been looking for the work on persistent real-time catenable deques. There are various approaches that have logarithmic complexities for concatenation of deques, and some that have amortized constant-time implementation, but much fewer real-time…

gasche
- 31,259
- 3
- 78
- 100
12
votes
2 answers
Why is Pypy's deque so slow?
Here is a (slightly messy) attempt at Project Euler Problem 49.
I should say outright that the deque was not a good choice! My idea was that shrinking the set of primes to test for membership would cause the loop to accelerate. However, when I…

Benjamin Hodgson
- 42,952
- 15
- 108
- 157
11
votes
5 answers
How Do I define a Double Brackets/Double Iterator Operator, Similar to Vector of Vectors'?
I'm porting code that uses a very large array of floats, which may trigger malloc failures from c to c++. I asked a question about whether I should use vectors or deques and Niki Yoshiuchi generously offered me this example of a safely wrapped…

Jason R. Mick
- 5,177
- 4
- 40
- 69
11
votes
3 answers
std::deque: How do I get an iterator pointing to the element at a specified index?
I have a std::deque, and I want to insert an element at a specified index (I'm aware that std::list would be better at this). The deque::insert() function takes an iterator to specify the location to insert. Given an index, how can I get an…

Ptah- Opener of the Mouth
- 113
- 1
- 4
11
votes
1 answer
android application dequeuebuffer error on snapdragon device
I got some strange crash when running our android application on snapdragon device, but thing works well on other devices.
Here are some logs before process die:
W/Adreno-EGLSUB( 3075): : dequeue native buffer fail: Unknown error…

user2824503
- 111
- 1
- 6
11
votes
1 answer
Best way to obtain indexed access to a Python queue, thread-safe
I have a queue (from the Queue module), and I want to get indexed access into it. (i.e., being able to ask for item number four in the queue, without removing it from the queue.)
I saw that a queue uses a deque internally, and deque has indexed…

Ram Rachum
- 84,019
- 84
- 236
- 374
11
votes
4 answers
Vector vs Deque insertion in middle
I know that deque is more efficient than vector when insertions are at front or end and vector is better if we have to do pointer arithmetic. But which one to use when we have to perform insertions in middle.? and Why.?

user1543957
- 1,758
- 4
- 20
- 30
10
votes
1 answer
Time complexity: deleting element of deque
What is the time complexity of deleting an element of collections.deque?
E.g.:
deq = collections.deque([1, 2, 3])
del deq[1]

Max Mikhaylov
- 772
- 13
- 32
10
votes
3 answers
Why is adding to or removing from the middle of a collections.deque slower than lookup there?
This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque object:
A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than…

Eli Rose
- 6,788
- 8
- 35
- 55
10
votes
2 answers
How does deque have an amortized constant Time Complexity
I read here from the accepted answer that a std::deque has the following characteristic
1- Random access - constant O(1)
2- Insertion or removal of elements at the end or beginning - amortized constant O(1)
3- Insertion or removal of elements -…

Rajeshwar
- 11,179
- 26
- 86
- 158
10
votes
2 answers
will stl deque reallocate my elements (c++)?
Hi I need an stl container which can be indexed like a vector but does not move old elements in the memory like a vector would do with resize or reserve (Unless I call reserve once at the beginning with a capacity enough for all elements, which is…

user1132655
- 243
- 4
- 14
10
votes
4 answers
How to release memory from std::deque?
I'm using a std::deque to store a fairly large number of objects. If I remove a bunch of those objects, it appears to me that its memory usage does not decrease, in a similar fashion to std::vector.
Is there a way to reduce it? I know that in a…

Peter
- 7,216
- 2
- 34
- 46
9
votes
6 answers
What is a data structure that has O(1) for append, prepend, and retrieve element at any location?
I'm looking for Java solution but any general answer is also OK.
Vector/ArrayList is O(1) for append and retrieve, but O(n) for prepend.
LinkedList (in Java implemented as doubly-linked-list) is O(1) for append and prepend, but O(n) for…

Randy Sugianto 'Yuku'
- 71,383
- 57
- 178
- 228
9
votes
0 answers
Is the libc++ implementation of the STL deque push_front function standards-compliant?
The C++ Standard (N4901) says this with reference to the deque push_front function:
An implementation shall provide these operations for all container types shown in the “container” column, and shall implement them so as to take amortized constant…

1f604
- 245
- 1
- 7