c++ vector/linked list hybrid
-
25-10-2019 - |
Question
Is there a std container in c++ that acts like a hybrid between a vector and a linked list. What I mean is a data structure that overcomes the frequent reallocation overhead of std::vector and potential excess memory allocation, instead when the structure runs out of space it adds a pointer to the next allocated fragment, and only when the number of fragments reaches a certain value, the entire structure is de-fragmented into a continuous new chunk and number of fragments is set back to 0.
Solution
std::deque
is the closest standard container to what you describe. It is not exactly like this, however (for example, it pretty much has to be an array of arrays rather than a list of arrays since the latter wouldn't permit constant-time element access).
Depending on your practical requirements, it might be close enough.
OTHER TIPS
As already said, std::deque
comes close to your requirements. I just want to add this comparison between std::vector
and std::deque
which I found very helpful. An In-Depth Study of the STL Deque Container