Finding the key from the resource in a std::map

Started by
4 comments, last by mind in a box 13 years, 9 months ago
He all!

Can I somehow find the Key-Value in a std::map when I have the resource it holds?

Here is my scenario:
TheMap[L"Stone.png"]=SomeTexture;


but the blocks of my game only get the pointer to the texture. Now I want to save the world and I need to know the texture they have got.
Advertisement
Two of your options are to use a boost bimap container or just iterate over your map until you find the element that contains your value.
Or, instead of a map you could stores the string 'key' value within the texture and use a std::set instead. That way you still just store pointers to the texture in the blocks, but they can follow this pointer to get to the key string.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Or make a second map PointerToName[SomeTexture] = L"Stone.png";

Or make the name a member variable of the texture...
Or just store the string that the block was created with.
Thank you for all the suggestions! [smile]
I implemented SiCrane's approach. The std::map isn't very big, so it's okay to loop it for each block one time.

This topic is closed to new replies.

Advertisement