Questions tagged [deque]

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

993 questions
50
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,100
  • 4
  • 21
  • 25
50
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
  • 8,646
  • 19
  • 59
  • 80
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
  • 8,179
  • 6
  • 31
  • 56
37
votes
2 answers

How to slice a deque?

I've changed some code that used a list to using a deque. I can no longer slice into it, as I get the error: TypeError: sequence index must be integer, not 'slice' Here's a REPL that shows the problem. >>> import collections >>> d =…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
33
votes
6 answers

c# equivalent for c++ vector or deque

I am almost certain this should be a duplicate but I searched for some time and could not find the answer. What should I use in C# to replace C++ vector and deque efficiently. That is I need a structure that supports direct indexing effieciently and…
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
30
votes
4 answers

Implement an immutable deque as a balanced binary tree?

I've been thinking for a while about how to go about implementing a deque (that is, a double-ended queue) as an immutable data structure. There seem to be different ways of doing this. AFAIK, immutable data structures are generally hierarchical, so…
30
votes
1 answer

C++ deque: when iterators are invalidated

Please correct me if I am wrong. Thank you! insert and erase will relocate elements, but elements before the position where insertion/erasure takes place don't relocate and hence their iterators remain valid. push_back and pop_back don't invalidate…
pic11
  • 14,267
  • 21
  • 83
  • 119
27
votes
2 answers

"move" two vectors together

If I have two vectors and want to combine them to one, I can do it the following way: std::vector a(100); // just some random size here std::vector b(100); a.insert(std::end(a), std::begin(b), std::end(b)); That involves copying though,…
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
27
votes
7 answers

Why does push_back or push_front invalidate a deque's iterators?

As the title asks. My understanding of a deque was that it allocated "blocks". I don't see how allocating more space invalidates iterators, and if anything, one would think that a deque's iterators would have more guarantees than a vector's, not…
rlbond
  • 65,341
  • 56
  • 178
  • 228
26
votes
2 answers

How can I pop() lots of elements from a deque?

I have a deque object what holds a large amount of data. I want to extract, say, 4096 elements from the front of the queue (I'm using it as a kind of FIFO). It seems like there should be way of doing this without having to iterate over 4096 pop…
Ross W
  • 1,300
  • 3
  • 14
  • 24
26
votes
3 answers

Is this deque thread-safe in python?

I can't decide whether the following deque is thread-safe. In short, I've created a class with a deque that displays its contents every 1 sec in a new thread (so it won't pause the main program while printing). The deque is filled from the main…
user1102018
  • 4,369
  • 6
  • 26
  • 33
23
votes
1 answer

How is sort for std::deque implemented?

Not so far I've learned how std::deque is implemented under the hood, and discovered that it's something like an array of pointers to n-byte arrays, where the data is actually stored. So now I have a couple of questions related to deques. A picture…
vortexxx192
  • 929
  • 1
  • 9
  • 24
20
votes
4 answers

Time complexity of removing items in vectors and deque

I have read that time complexity of adding items to end of a std::vector is amortized constant and inserting items at the top and bottom of a std::deque is constant.Since both these containers have a random access iterator thus accessing elements at…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
20
votes
3 answers

The difference between vector and deque

As vector and deque both provides a function to push_back the element at the last. where deque also provides a function push_front to insert the element at the beginning, which is bit costly in case of vector. My question is when we can achieve…
rajenpandit
  • 1,265
  • 1
  • 15
  • 21
19
votes
3 answers

std::deque memory usage - Visual C++, and comparison to others

Follow up to What the heque is going on with the memory overhead of std::deque? Visual C++ manages deque blocks according to the container element type using this: #define _DEQUESIZ (sizeof (value_type) <= 1 ? 16 \ : sizeof (value_type) <= 2 ?…
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
1
2
3
66 67