Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which data structure would be best for this?

For my game, when an object enters a sensor, I need to add it to a list, and when the object leaves the sensor it needs to be removed from that list. I also need to quickly be able to find that object.

So essentially:

I need it to do: quick adding, quick removal and quick find.

What sort of data structure would be best for this given that at any given time, the stucture will have about 10 objects.

Thanks

like image 943
jmasterx Avatar asked Jul 31 '11 16:07

jmasterx


1 Answers

With 10 objects anything will do (std::vector, deque or set), and nobody can tell which one performs better before profiling.

If you don't know what to use, perhaps you'll find that std::set has a nicer syntax for looking up elements. This is what I would use in this situation, because I wouldn't like to write std::find(v.begin(), v.end(), sensor) where I could simply write s.find(sensor).

Don't use std::list though, as a general advice. You need a compelling reason to use linked lists in C++ (constant time splicing is one, absence of iterator invalidation is another). Other data structures perform better for most operations (except splicing). Here, I don't see any point in using list rather than eg. set.

like image 170
Alexandre C. Avatar answered Sep 21 '22 15:09

Alexandre C.