There's more...

Internally, the VecDeque is implemented as a ring buffer, also known as a circular buffer. It's called like this because it behaves like a circle: the end touches the beginning.

It works by allocating a continuous block of memory, like the Vec; however, where the Vec always leaves its extra capacity at the end of the block, the VecDeque has nothing against leaving spots inside the block empty. It follows that when you remove the first element, the VecDeque doesn't move all elements to the left, but simply leaves the first spot empty. If you then push an element into the beginning via push_front, it will take the spot freed earlier while leaving the elements after it untouched.

The circular catch in the story is that if you have some capacity in the front of the block but none in the back while using push_back, the VecDeque will simply use that space to allocate the extra element, leading to the following situation:

This is great, because you will not have to worry about this at all while using it, as its iterating methods hide the implementation by always showing you the correct order!

Like the vector, VecDeque will resize itself and move all its elements into a new block when its capacity runs out.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.251.163