Vector vs List

Does anyone know what is the main difference between Vector Template and List Template.

It seems to me quite similar....maybe It's just that list can be access to any point straight...I know I coudl read the documentntion but I would need like a Highlight!!

The documentation is pretty straightforward:

 http://www.cplusplus.com/reference/vector/vector/ wrote:
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.
Internally, vectors use a dynamically allocated array to store their elements.


 http://www.cplusplus.com/reference/list/list/ wrote:
List containers are implemented as doubly-linked lists;


That is, a vector is an array, a list is a linked list.

Hope this helps.
vector elements are stored sequentially in memory. List elements could be stored in different parts in memory.
A vector is also considered a random access container, where a list is not.

maybe It's just that list can be access to any point straight...

If I understand you correctly, it's the other way round.

With a vector you can use operator[] to access any element directly (by index). Because, as jlb said, vector is a random access container.

With list you got to start with the head or tail element and then walk the list elements until you get to the one you want. Because list is a sequence container.

And yes, you should read the doumentation!!

Andy
Last edited on
Earlier this year I went for 5 job interviews in the space of two weeks. Interestingly (or maybe not) three of those companies asked me this question :)
Thanks to everyone, I'm reading the documentation and I think I'm hitting the point...
Mutexe It's not related to an intereview but It's similar....I have selected several questions that are important to have a reasonable undertanding of C++, and I studing C++ following thoses "main things",but It's good to know it....
Topic archived. No new replies allowed.