A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.
Questions tagged [deque]
993 questions
6
votes
1 answer
delete the hashed node in deque in O(1)
How do I hash a built-in node in deque (which is a double linked list) and delete the node in the middle in O(1)? Is the built-in node exposed?
For example, I want to save a deque's node in dict so I can delete the node in constant time later.
This…

Denly
- 899
- 1
- 10
- 21
6
votes
2 answers
How to get deque elements via indices?
I have the following deque object:
test = deque([np.zeros((4,1,1))+0.5] * 25)
So there are 25 arrays of some shape, and I will be appending in objects, popping off old ones on the other end, etc.
At some point I will want to select a subset of…

JDS
- 16,388
- 47
- 161
- 224
6
votes
1 answer
Deque vs Queue Speed
I was working on a problem on LeetCode (Here). When I finished the problem, I came up with:
class MovingAverage {
std::deque numsToAverage;
int maxSize;
int currentTotal;
public:
/** Initialize your data structure here. */
…

JulianSullivan
- 77
- 1
- 2
6
votes
4 answers
Check maxlen of deque in python 2.6
I have had to change from python 2.7 to 2.6.
I've been using a deque with the maxlen property and have been checking what the maxlen is. Apparently you can use maxlen in python 2.6, but in 2.6 deques do not have a maxlen attribute.
What is the…

simonb
- 2,777
- 2
- 18
- 16
6
votes
4 answers
Bug with std::deque?
I am trying to delete an element from a deque using a loop and an iterator. I am following online examples but seeing a bug.
I am using g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9).
Here is the code:
#include
#include
using…

Trenin
- 2,041
- 1
- 14
- 20
6
votes
1 answer
Why not always use circular array deques instead of array lists?
Almost all programming languages have some implementation of a list that uses a dynamic array, which automatically expands when it reaches a certain capacity. For example, Java has ArrayList, and C++ has std::vector.
Recently I learned about…

gengkev
- 1,890
- 2
- 20
- 31
6
votes
1 answer
Why does deque.extendleft() reverse the order of elements?
From official documentation:
extendleft(iterable)
Extend the left side of the deque by appending elements from iterable. Note, the
series of left appends results in reversing the order of elements in the iterable argument.
What is the…

Andrzej Sołtysik
- 300
- 5
- 18
6
votes
2 answers
What's the equivalent std::deque in Qt?
I am on several lines codes from a Qt project to appending into a Qvector every 1s. I noticed that in STL deque could have a better performance in adding a new element into the end that vector. What's the equivalent or similar Qt way? Cause I don't…

colddie
- 1,029
- 1
- 15
- 28
6
votes
2 answers
Using std::deque::iterator (in C++ STL) for searching and deleting certain elements
I have encountered a problem invoking the following code:
#include
using namespace std;
deque deq = {0,1,2,3,4,5,6,7,8};
for(auto it = deq.begin(); it != deq.end(); it++){
if(*it%2 == 0)
deq.erase(it);
}
which resulted in…

William Huang
- 145
- 1
- 3
- 6
6
votes
3 answers
Python deque scope?
I am learning Python and have been trying to make a deque. However, I get incorrect output and I am not sure why. My code is as follows:
p = [2, 1], [1, 1]
init_q= deque()
init_q.append(p)
for i in range(len(p)):
for j in range(len(p[i])):
…

Sjieke
- 384
- 1
- 2
- 9
5
votes
5 answers
std::vector< std::vector > or std::deque< std::vector >?
I have an existing algorithm and I need to optimize it sligtly if it is possible. Changing a lot in this algorithm is not an option at the moment. The algoritm works with instance of std::vector< std::vector >. It looks like…
user184968
5
votes
4 answers
What is the best way to access deque's element in C++ STL
I have a deque:
deque My_Deque;
My_Path.push_front('a');
My_Path.push_front('b');
My_Path.push_front('c');
My_Path.push_front('d');
My_Path.push_front('e');
There are such ways to output it.
The first:
deque::iterator It;
for ( It =…

Lucky Man
- 1,488
- 3
- 19
- 41
5
votes
1 answer
std::deque is contiguous memory container or not?
std::deque is contiguous memory container or not ?
The famous book Effective STL by Scott Meyers says like below
Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks…

myoldgrandpa
- 871
- 7
- 21
5
votes
1 answer
Count the number of ranges [L; R] which has difference between the maximum and minimum is even
Given an array n elements (n <= 10^5) Count the number of ranges [L; R] (L <= R) which has difference between the maximum and minimum is even.
For example, n = 5
a[] = {4, 5, 2, 6, 3}
The answer is 11: [1;1], [1;4], [1;5], [2;2], [2;4], [2;5],…

Hiển Nguyễn Minh
- 303
- 1
- 9
5
votes
3 answers
What is the Java equivalent of C++ deque?
in C++ all I had to do was
#include -> including
queue a; -> defining
a.push(1); ->using
but in java I found very difficult to use simple deque
what should I do...?
more specifically,
How should I code to simply do the same steps
as…

hongtaesuk
- 557
- 5
- 10
- 19