Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What's the difference between stl vector and stl list?


Asked by Shay Reed on Dec 12, 2021 FAQ



The std::list command is provided as the template class, storing any type of object similar to the std::vector. We can store new elements into the std::list object using the push_back or push_front member functions, which all have constant time complexity. As for the std::vector, we only have push_back, which has the average constant complexity.
Moreover,
Hence std::list provides some extra functions for Sorting, Splicing, Removing elements and identifying unique elements. Vector provides the random access and hence can be used with STL algorithms that uses Random Access Iterators.
Also, Vector is faster for insertion and deletion of elements at the end of the container. Set is faster for insertion and deletion of elements at the middle of the container. Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.
In fact,
As std::list do not provide random access, there many STL algorithms that uses Random Access Iterators can not be used with List. Hence std::list provides some extra functions for Sorting, Splicing, Removing elements and identifying unique elements.
One may also ask,
In this article we will discuss the differences between vector and deque STL Containers. Vector provides insertion and deletion at middle and end only. Whereas, deque provides operations for insertion at front, middle and end.