Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » C++ » Difference between C++ Vector & List

What is difference between C++ Vector and List

C++ Vector and List both are standard template library, also known as sequencial containers. Vector store elements at contiguous memory while List stores elements at non contiguous memory.

Top 10 differences in Vector and List of C++

  • Vector has contiguous memory
  • List has noncontiguous memory
  • Vector Pre allocates space for future
  • List doesn’t pre allocates space for future
  • A list is not synchronized while a vector is
  • Lists have no default size while a vector has a default size of 10
  • Lists and vectors are both dynamically growing arrays
  • A list is not thread safe whereas a vector is thread safe
  • Lists, as they apply only to addition and deletion in the front and rear, are faster while vectors take more CPU
  • 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.