In general, use vector when you don't care what type of sequential container that you're using, but if you're doing many insertions or erasures to and from anywhere in the container other than the end, you're going to want to use list. Or if you need random access, then you're going to want vector, not list.
A list holds different data such as Numeric, Character, logical, etc. Vector stores elements of the same type or converts implicitly. Lists are recursive, whereas vector is not. The vector is one-dimensional, whereas the list is a multidimensional object.
List stores elements at non contiguous memory location i.e. it internally uses a doubly linked list i.e. Whereas, vector stores elements at contiguous memory locations like an array i.e.
Vectors are not linked linked list, they provide random access and are contiguous just like arrays.
vector:
list:
In general, use vector when you don't care what type of sequential container that you're using, but if you're doing many insertions or erasures to and from anywhere in the container other than the end, you're going to want to use list. Or if you need random access, then you're going to want vector, not list. Other than that, there are naturally instances where you're going to need one or the other based on your application, but in general, those are good guidelines.
Situations where you want to insert a lot of items into anywhere but the end of a sequence repeatedly.
Check out the complexity guarantees for each different type of container:
What are the complexity guarantees of the standard containers?
If you don't need to insert elements often then a vector will be more efficient. It has much better CPU cache locality than a list. In other words, accessing one element makes it very likely that the next element is present in the cache and can be retrieved without having to read slow RAM.
Most answers here miss one important detail: what for?
What do you want to keep in the containter?
If it is a collection of int
s, then std::list
will lose in every scenario, regardless if you can reallocate, you only remove from the front, etc. Lists are slower to traverse, every insertion costs you an interaction with the allocator. It would be extremely hard to prepare an example, where list<int>
beats vector<int>
. And even then, deque<int>
may be better or close, not justyfing the use of lists, which will have greater memory overhead.
However, if you are dealing with large, ugly blobs of data - and few of them - you don't want to overallocate when inserting, and copying due to reallocation would be a disaster - then you may, maybe, be better off with a list<UglyBlob>
than vector<UglyBlob>
.
Still, if you switch to vector<UglyBlob*>
or even vector<shared_ptr<UglyBlob> >
, again - list will lag behind.
So, access pattern, target element count etc. still affects the comparison, but in my view - the elements size - cost of copying etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With