How to make a fast search for an object with a particular value in a vector of structs or classes? c++

前端 未结 4 2019
无人及你
无人及你 2021-01-24 06:38

If I have thousands of struct or class objects in a vector, how to find those that are needed, in a fast way?
For example:
Making a game, and I need fas

4条回答
  •  天涯浪人
    2021-01-24 07:18

    i think you have to go more in depth that the simple research of a value inside a group of struct, even more if you are planning on searching among a elevated number. How are the struct generated, how are they collected and how you keep track of them, there is a common key that you can you can use to order while you create them?

    You should focus on sorting them while you add it to the whole structure, that way you avoid massive computation burst every time you have to perform a search. Choose a good algorithm (example AVL sorting), that way you can have a O(log(n))) adding/delete/searching.

提交回复
热议问题