Pergunta de entrevista da empresa UST

What is the difference between std::list and std::vector

Resposta da entrevista

Sigiloso

23 de mai. de 2025

std::vector has an underlying array implementation, which allocates a contiguous block of memory to store the data. Once this block is exhausted, it reallocates a bigger block with double the capacity of the current one and copies the entire data to this new block. Since data is stored in a contiguous block of memory, index-based access to an element is possible. It's fast to insert and delete from the very end, but takes time to insert and delete at the beginning and middle. std::list has an underlying doubly linked list implementation, which makes index-based access impossible and requires traversal to get to the element. It has a similar insertion and deletion speed across the data. It also costs us a memory overhead to store the pointers to the previous and next elements.

1