list vs vector

Started by
11 comments, last by maximAL 18 years, 4 months ago
sometimes i dont know if i should use a list or a vector. both serve the same features, but from the STL documentation: "Lists are sequences of elements stored in a linked list. Compared to vectors, they allow fast insertions and deletions, but slower random access." "Vectors contain contiguous elements stored as an array. Accessing members of a vector or appending elements can be done in constant time, whereas locating a specific value or inserting elements into the vector takes linear time." so, what do they mean by "random access"? i'm developing a simple mesh data structure. which one should i use to store list of indices, which probably never change, but they are accessed very often. thanks!
Advertisement
Hi Bud,

Random access basically means that you can use the [] operator on the container to get at a specific element. You can use std::sort() on the vector because the std::sort() requires random access.

I always use vectors unless i find lists to be more appropriate.

Hope that helps,

ace
"random access" means that you can access any element directly. In contrast, a list has "sequential access", which means that to access an element you have to start at the beginning and go through each element until you get to the one you want to access.

In your case, a list is probably not appropriate, since fast insertion and deletion is not important. There are other containers, but a vector is likely to be the right one.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
thanks guys, i think i understand it now!
In case you're still uncertain, for more information about the containers and how they work:
http://en.wikipedia.org/wiki/Dynamic_array (std::vector)
http://en.wikipedia.org/wiki/Linked_list (std::list is a doubly linked list)
The above links will help you understand the performance characteristics of these two container types.
Image Hosted by ImageShack.us
------------------------------------------------------------Jawohl, Herr Oberst!
Haha, that's pretty cool .
Rob Loach [Website] [Projects] [Contact]
That image alone should be stickied :)
I bookmarked it.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Now that is a useful image. I'm printing that and sticking it in people's cubes at work or something.

This topic is closed to new replies.

Advertisement