C ++ vector / linked list hybrid

Is there a std container in C ++ that acts like a hybrid between a vector and a linked list. I mean a data structure that overcomes the frequent overhead of std :: vector reassignment and the potential excessive allocation of memory, 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, all the structure is defragmented into a continuous new piece, and the number of fragments is set to 0.

+5
source share
2 answers

std::dequeis the closest standard container to what you are describing. However, this is not entirely true (for example, to a large extent it should be an array of arrays, not a list of arrays, since the latter does not allow access to the element with constant time).

Depending on your practical requirements, this may be close enough.

+3
source

As already mentioned, it std::dequeapproaches your requirements. I just want to add this comparison between std::vectorand std::deque, which I found very useful. STL Deque Container Depth Study

+4
source

All Articles