Questions tagged [deque]

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

993 questions
9
votes
2 answers

Does std::deque actually have constant time insertion at the beginning?

The Standard says: A deque is a sequence container that supports random access iterators (27.2.7). In addition, it supports constant time insert and erase operations at the beginning or the end; insert and erase in the middle take linear…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
9
votes
4 answers

Why is Deque (ArrayDeque) capacity a power of two?

In Java (but similarly in PHP) the ArrayDeque implementation always has its capacity as a power of 2: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/ArrayDeque.java#l126 For HashMap this choice is clear - to…
radistao
  • 14,889
  • 11
  • 66
  • 92
9
votes
3 answers

ArrayDeque is implemented as an array, why is it not Random Access?

I know that ArrayDeque is fast when adding and removing simple lists. I tested it, it was quicker to add and delete than LinkedList. Because I know that it is implemented as an Array, so why not Random Access? I read the ArrayDeque.java file in the…
Taylous
  • 260
  • 4
  • 15
9
votes
4 answers

Why is an STL deque not implemented as just a circular vector?

I always thought that in C++ standard template library (STL), a double-ended queue (deque) is a size-variable array (like a vector) with circular boundary conditions, meaning there's a head pointer i and a tail pointer j both pointing to some…
Zhuoran He
  • 873
  • 9
  • 15
9
votes
4 answers

Difference between add(E e) and offer(E e) of ArrayDqueue Class

Hi I used add and offer to add my element in last pace. Both are returning boolean and both does not throw any exception apart from NPE. public class ArrayDequeDemo { public static void main(String[] args) { // Create ArrayDeque elements. …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
9
votes
1 answer

Move an element from std::deque in C++11

We know that std::deque::front() return a reference to a first element of deque. I would to know if this code is always safe: //deque of lambdas deque> funs; // then is some other place: // take a lock m.lock(); auto f =…
Gian Lorenzo Meocci
  • 1,136
  • 2
  • 14
  • 23
9
votes
1 answer

C++ deque's iterator invalidated after push_front()

Just now, I'm reading Josuttis' STL book. As far as I know -- c++ vector is a c-array that can be reallocated. So, I understand, why after push_back() all iterators and references can become invalid. But my question is about std::deque. As I know it…
f0b0s
  • 2,978
  • 26
  • 30
9
votes
5 answers

Why is deque using so much more RAM than vector in C++?

I have a problem I am working on where I need to use some sort of 2 dimensional array. The array is fixed width (four columns), but I need to create extra rows on the fly. To do this, I have been using vectors of vectors, and I have been using some…
Zac-K
  • 550
  • 5
  • 17
9
votes
4 answers

dequeueReusableCellWithIdentifier always returns nil (not using storyboard)

I am creating the cell programatically using the reuse identifier. Note - I am not using storyboard for creating the cell Whenever the cell is dequeued, the cell is nil, so the cell needs to be newly created using alloc, which is expensive. EDIT…
user1046037
  • 16,755
  • 12
  • 92
  • 138
8
votes
1 answer

Most efficient method of copying std::deque contents to byte-array

Is there a better way to copy the contents of a std::deque into a byte-array? It seems like there should be something in the STL for doing this. // Generate byte-array to transmit uint8_t * i2c_message = new uint8_t[_tx.size()]; if ( !i2c_message )…
Zak
  • 12,213
  • 21
  • 59
  • 105
8
votes
1 answer

How to control the chunk size of `std::deque` when allocating a new chunk?

When we insert a new element into a std::deque, it may allocate a new chunk to contain the element if the existing chunks are all full. However, how does the implementation control the chunk size? Is it possible for the user to control the chunk…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
8
votes
2 answers

How to get the item to be discarded in python deque?

Suppose we have a deque with maxlen=3. If the deque already have 3 items and when I append a new item, how can I get the item that's going to be discarded? The reason is that I want to maintain a window in memory that only contains the last N item,…
yegle
  • 5,795
  • 6
  • 39
  • 61
8
votes
7 answers

How to "convert" a dequed object to string in Python?

I am trying to output a rotated version of a string. I have taken a string, z="string", and created a deque out of it, y=collections.deque(z) (deque(['S','t','r','i','n','g']), and rotated it using the rotate method. How do I "convert" that deque…
Stephen Paul
  • 2,762
  • 2
  • 21
  • 25
7
votes
4 answers

Complexity of stl deque::insert()

I learned the complexity of deque::insert() from the C++ standard 2003 (chapter 23.2.1.3) as follows: In the worst case, inserting a single element into a deque takes time linear in the minimum of the distance from the insertion point to the…
Danqi Wang
  • 1,597
  • 1
  • 10
  • 28
7
votes
1 answer

Why is `std::deque` not `constexpr` friendly?

I'm learning the c++ STL and it came to my attention that while most functionalities supported by std::vector and std::array (contiguous storage) are marked with constexpr, that's not the case for std::deque and other non-contiguous storages. So I…