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
, 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With