Under the hood, a std::deque
is typically implemented as a sequence of contiguous chunks of memory, sometimes called "blocks" or "pages".
Each block holds a fixed number of elements. The deque maintains a dynamic array of pointers, where each pointer points to one of these blocks.
This structure provides several advantages:
std::vector
 when inserting at the front.However, this structure is not as cache-friendly as a std::vector
's contiguous memory, which can impact iteration and random access performance.
Here's a simplified example of how a deque might be implemented:
template <typename T>
class Deque {
T** blocks_;
size_t frontBlock_;
size_t backBlock_;
// ...
public:
T& operator[](size_t index) {
size_t block =
frontBlock_ + (index / blockSize);
size_t offset = index % blockSize;
return blocks_[block][offset];
}
// ...
};
Answers to questions are automatically generated and may not have been reviewed.
std::deque
A guide to double-ended queues - a structure that behaves like a vector, specialised for manipulating objects at the edges of the collection