Vector vs List

May 21, 2015 at 9:48pm
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!!

May 21, 2015 at 10:04pm
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.
May 21, 2015 at 10:06pm
vector elements are stored sequentially in memory. List elements could be stored in different parts in memory.
May 21, 2015 at 10:12pm
A vector is also considered a random access container, where a list is not.

May 22, 2015 at 4:37am
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 May 22, 2015 at 4:39am
May 22, 2015 at 7:53am
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 :)
May 22, 2015 at 7:48pm
Thanks to everyone, I'm reading the documentation and I think I'm hitting the point...
May 22, 2015 at 7:57pm
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.