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
With 10 objects anything will do (
std::vector,dequeorset), and nobody can tell which one performs better before profiling.If you don’t know what to use, perhaps you’ll find that
std::sethas a nicer syntax for looking up elements. This is what I would use in this situation, because I wouldn’t like to writestd::find(v.begin(), v.end(), sensor)where I could simply writes.find(sensor).Don’t use
std::listthough, 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 usinglistrather than eg.set.