Sign in to follow this  
ehmdjii

list vs vector

Recommended Posts

ehmdjii    238
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!

Share this post


Link to post
Share on other sites
dave    2187
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

Share this post


Link to post
Share on other sites
JohnBolton    1372
"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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Now that is a useful image. I'm printing that and sticking it in people's cubes at work or something.

Share this post


Link to post
Share on other sites
Boku San    428
Quote:
Original post by maximAL
(OMGAWESOMEIMAGE)


I'm printing this, saving it, and rate++ing you. Recently I decided to use the standard containers a bit more, and (as some from #gamedev will know), I've been talking about them a lot. And, even though it hasn't been much of a problem recently, I'm still occasionally looking up capabilities of the containers.

In short -- I LOVE YOU.

Share this post


Link to post
Share on other sites
maximAL    229
Quote:
Original post by Alpha_ProgDes
Quote:
Original post by maximAL
awesome image


so what did you use to create that flowchart?

well, thats not mine [depressed]
found it somewhen somewhere, short search brought me http://www.software.net.nz/ as possible source...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this