Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ vector/linked list hybrid

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.

like image 720
dtech Avatar asked Nov 29 '11 13:11

dtech


2 Answers

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

like image 52
Mythli Avatar answered Sep 20 '22 02:09

Mythli


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.

like image 45
NPE Avatar answered Sep 21 '22 02:09

NPE