6/22/08

What is the difference between list and vector?

STL Question: What is the difference between list and vector?

Answer:


list and vector implements fundamentally different data structures.
The major difference is the way of element access. Vector elements may accessed randomly while list elements must be accessed sequentially.

Note that vectors are dynamic array, they can grow in size just like lists. But for in between insertions elements needs to be moved along with memory allocation and swapping.

Second is the frequent insertion and deletion of elements is very expensive in case of vectors while it is efficient in case of vectors.

Iterators are different as a result of the first item: list support bi-directional iterators while vector uses random access iterator.
Some methods are difference (for example, only list supports merge and splice, adds push_front and pop_front etc.

No comments:

ITUCU