std::list vs std::vector

For my program I need a way to store my in-game objects. Until now I always used vector, but the I realized that I never need to actually use the [] operator and I simply always iterated over the vector. Now my question: would it be better to use a list here?

Thanks!
That depends on if you're constantly changing around the ordering of the items in the middle of your container. std::lists (being double-linked lists) are very good at that, while std::deques aren't so good and std::vectors are horrible.

Keep in mind that you'll suffer a slight memory usage hit with std::lists, as in addition to the actual object, per element the list will also need to store two pointers.

Good luck.

-Albatross
Last edited on
Nothing is being changed, ever.
Then vector will be (much) faster and use less memory (well, the latter might not apply in case the vector has a lot of unused capacity and the stored type has a very large structure size - but that's rare and even then, it can be fixed by choosing the right capacity).
lists dynamically allocate each of their nodes, so the elements can be strewn wide and far in memory, while vector stores them compactly. The CPU cache will thank you for that.
Last edited on
Ok thanks!
Topic archived. No new replies allowed.