Jump to content
  • Advertisement
Sign in to follow this  
ehmdjii

list vs vector

This topic is 4616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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

Share this post


Link to post
Share on other sites
"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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!