During my daily work, I am always advised by senior member of the team that list is not cache friendly so I should vector
. I understand that list
is not continuous so that the memory allocation is scattered around the whole memory.
However, very often I do need the functionality of a list
(or a map
). So I am wondering if I can write my own allocator, which is a vector
underneath. Every time when I push_back
, my own allocator will allocate a new item from a per-allocated vector
.
When I travel the list
/map
, the cache locality is preserved.
Does this make sense to any of you?
std::list
isn't an associative container. – Kyanitevector
? What does this construction give you that avector
doesn't? If you try to use any of the functionality that's ordinarily more efficient on alist
than avector
, you'll leak memory. – Bertylist
overvector
is O(1) insert and remove. I can't think of a way to get this and guarantee decent spatial locality. – Paradoxstd::list
overstd::vector
is that removing from the middle ofstd::list
doesn't invalidate iterators. If you don't need that property of lists then I'd suggest using a vector instead. – Byrnielist
-like, but the details of arranging and finding the elements depend strongly on which operations you want to optimize. That said, your seniors need to wait until the product is profiled, or you will end up spending unnecessary time and effort deciding and refining those details. Unless you really have a boat, don't re-invent the wheel. – Injurious