In: Computer Science
Discuss tradeoffs between using the C++ STL list and vector.
STL stands for Standard Template Library. it is a library in the C++ language.
Actually, please use vector when you don't care about what type of sequential container that you're using, but if you're doing many insertions and from anywhere in the container other than the end, you should use list. if you require random access, then you have to use vector. You can find more about this in the below table...
vector | C++ STL list |
Elements are stored in Contiguous memory | Non-contiguous memory |
Pre-allocated memory, | No pre-allocated memory |
Each element only needs the space for the element type itself. | Each element needs extra space for the node which holds the element |
re-allocate memory for the entire vector any time that you add an element is possible. | can't re-allocate memory |
randomly access its elements. | can't randomly access elements. |
Iterators are disproving if you add or delete elements to or from the vector. | Iterators remain valid even when you add or delete elements from the list. |
PLEASE LIKE...THANK YOU!