简体   繁体   中英

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

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 .

我建议使用std::list因为它非常适合插入和删除元素。

Quick adding, quick removing and quick finding, you don't want much do you! Given what you said I would say linked list, but it also depends on how frequent adds, deletes and finds are. If you're finding much more frequently than you are adding or deleting things change.

Really the only way is to try a few different choices and time them.

I think a Linked List would be best. Given the small size, the act of shifting pointers would not impact performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM