I am looking the most ideal data structure for inserting and deleting objects that can be located anywhere in the data structure and can be done efficiently and fast. I have tried vector, however you need to iterate through it a lot to select a random object.
Asked
Active
Viewed 55 times
0
-
Do you need random access or will you only ever access this data by iterating from the start (or end?)? – MTCoster Dec 15 '21 at 09:45
-
That's not quite enough detail to select a specific structure. Is it a bunch of values (set), or a key-value mapping (map)? What operations do you need it to support? We got deleting an element, what does "select a random element" mean? Do you have some example code using vector to make things clearer? – Dec 15 '21 at 09:46
-
Random access would be ideal, I would like to access a set of data based on the type of object it is, for example I could retrieve all "type 2" data or just retrieve one object by it self or just all objects – steven33m3 Dec 15 '21 at 09:47
-
Does this answer your question? https://stackoverflow.com/q/471432/4117728. – 463035818_is_not_an_ai Dec 15 '21 at 09:52
-
from your comment about looking up stuff, sounds like you need some maps to augment your structure so that you reduce your iterations/searches. (Also, if you seek random, one does not need to iterate through a vector to select a random object... just generate a random index!) – Abel Dec 15 '21 at 09:53
-
Thank you for your help, that cheat sheet helps alot – steven33m3 Dec 15 '21 at 09:59
-
That cheat sheet, seems to miss std::unordered_map though – Pepijn Kramer Dec 15 '21 at 10:19