I have a std::vector<MyClass*>
(a vector of MyClass pointers), and I need to find a particular item in the vector. The class has a getID()
function which returns a unique identifier of the class.
If I want to find a class with a specific ID, I iterate through the vector looking for the class with the ID, like this:
for(std::vector<Chunk*>::iterator it = chunksRenderList.begin(); it != chunksRenderList.end(); ++it)
{
if((*it)->getID() == id)
return *it;
}
This is rather slow because I am calling this code lots of times per second. I have tried using a std::unordered_map
which was a lot slower, and a std::map
was slower again. I'm not sure why it was slower, maybe it's the way I used it.
Is there any container/algorithm I can use to access a particular item without having to iterate (that is faster than iteration)?