Questions tagged [deque]

A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.

878 questions
212
votes
8 answers

What really is a deque in STL?

I was looking at STL containers and trying to figure what they really are (i.e. the data structure used), and the deque stopped me: I thought at first that it was a double linked list, which would allow insertion and deletion from both ends in…
Zonko
  • 2,992
  • 2
  • 17
  • 26
195
votes
6 answers

Why should I use Deque over Stack?

I need a Stack data structure for my use case. I should be able to push items into the data structure and I only want to retrieve the last item from the Stack. The JavaDoc for Stack says : A more complete and consistent set of LIFO stack operations…
Geek
  • 23,609
  • 39
  • 133
  • 212
183
votes
8 answers

Why is ArrayDeque better than LinkedList

I am trying to to understand why Java's ArrayDeque is better than Java's LinkedList as they both implement Deque interface. I hardly see someone using ArrayDeque in their code. If someone sheds more light into how ArrayDeque is implemented, it…
Cruel
  • 1,833
  • 2
  • 12
  • 4
108
votes
9 answers

What's the difference between deque and list STL containers?

What is the difference between the two? I mean the methods are all the same. So, for a user, they work identically. Is that correct??
Lazer
  • 79,569
  • 109
  • 264
  • 349
90
votes
4 answers

How are deques in Python implemented, and when are they worse than lists?

I've recently gotten into investigating how various data structures are implemented in Python in order to make my code more efficient. In investigating how lists and deques work, I found that I can get benefits when I want to shift and unshift…
Eli
  • 31,424
  • 32
  • 127
  • 194
88
votes
10 answers

Why would I prefer using vector to deque

Since they are both contiguous memory containers; feature wise, deque has almost everything vector has but more, since it is more efficient to insert in the front. Why whould anyone prefer std::vector to std::deque?
Leon
  • 7,253
  • 10
  • 43
  • 49
62
votes
6 answers

Use slice notation with collections.deque

How would you extract items 3..6 efficiently, elegantly and pythonically from the following deque without altering it: from collections import deque q = deque('',maxlen=10) for i in range(10,20): q.append(i) the slice notation doesn't seem to…
Jonathan
  • 84,911
  • 94
  • 244
  • 345
62
votes
4 answers

python: deque vs list performance comparison

In python docs I can see that deque is a special collection highly optimized for poping/adding items from left or right sides. E.g. documentation says: Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short…
Oleg Tarasenko
  • 8,244
  • 17
  • 64
  • 97
55
votes
8 answers

Why do we need Deque data structures in the real world?

Can anyone give me an example of situation where a Deque data structure is needed? Note - Please don't explain what a deque is?
user366312
  • 17,582
  • 55
  • 198
  • 392
51
votes
5 answers

Why is std::vector so much more popular than std::deque?

Possible Duplicate: Why would I prefer using vector to deque I am curious why is it that std::vector is so much more popular than std::deque. Deque is almost as efficient in lookup, more efficient in insert (without vector::reserve)and allows for…
Karthik T
  • 29,587
  • 4
  • 58
  • 84
47
votes
2 answers

Add an object to the beginning of an NSMutableArray?

Is there an efficient way to add an object to start of an NSMutableArray? I am looking for a good double ended queue in objective C would work as well.
gurooj
  • 2,060
  • 4
  • 21
  • 25
43
votes
4 answers

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…
jasonline
  • 7,378
  • 18
  • 54
  • 77
42
votes
1 answer

Converting a deque object into list

Currently I fetch "list" data from my storage, "deque" it to work with that data. After processing the fetched data I have to put them back into the storage. This won't be a problem as long as I am not forced (at least I think so) to use Python's…
Julius F
  • 3,194
  • 4
  • 27
  • 40
41
votes
2 answers

Queue vs Dequeue in java

What is the difference between them? I know that A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue. Where as Dequeue represents a queue where you can insert and remove…
user6503581
37
votes
2 answers

Why does GCC -O3 cause infinite std::distance with filter iterators over a std::deque?

After much pain and misery, I've tracked down some very odd behaviour where std::distance never returns when given a range of boost::filter_iterators over a std::deque. It appears the problem is unique to GCC (6.1+) with -O3 optimisations. Here is…
Daniel
  • 6,951
  • 5
  • 25
  • 51
1
2 3
58 59