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.
Asked
Active
Viewed 1,971 times
6
-
Have you taken a look at `std::deque`? Does it fit your requirements? – Asha Nov 29 '11 at 13:08
-
I think `std::deque` works that way, but I'm not sure though. – Constantinius Nov 29 '11 at 13:08
-
@Constantinius I always thought `std::deque` was just a kind of ringbuffer and thus similar to a `std::vector` in its memory allocation behaviour. – Christian Rau Nov 29 '11 at 13:55
2 Answers
4
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

Mythli
- 5,995
- 2
- 24
- 31
3
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.

NPE
- 486,780
- 108
- 951
- 1,012
-
So I can basically use a deque and just add the few lines of code I need to defragment it once a certain level of fragments is reached. The idea is naturally to eliminate memory hops and cache misses... – dtech Nov 29 '11 at 13:21