CppBuzz.com
  
Home C C++ Java Python Perl PHP SQL Javascript Linux Forum

You are here : Home » C++ » Difference between C++ Vector and List

What is difference between C++ Vector and List


Top 10 differences in Vector and List of C++:

1.Vector has contiguous memory
2.List has noncontiguous memory.
3.Vector Pre allocates space for future.
4.List doesn’t pre allocates space for future.
5. A list is not synchronized while a vector is.
6. Lists have no default size while a vector has a default size of 10.
7. Lists and vectors are both dynamically growing arrays.
8. A list is not thread safe whereas a vector is thread safe.
9. Lists, as they apply only to addition and deletion in the front and rear, are faster while vectors take more CPU.
10. A vector grows by its size twice while a list decreases to half, i.e., 50 per cent.


To conclude, we can get some facts about each data structure (Vector & List):

std::vector is insanely faster than std::list to find an element
std::vector performs always faster than std::list with very small data
std::vector is always faster to push elements at the back than std::list
std::list handles very well large elements, especially for sorting or inserting in the front

This draw simple conclusions on usage of each data structure:

Number crunching: use std::vector
Linear search: use std::vector
Random Insert/Remove: use std::list (if data size very small (< 64B on my computer), use std::vector)
Big data size: use std::list (not if intended for searching) If you have the time, in practice, the best way to decide is always to benchmark both versions, or even to try another data structures.