Public Group

std::map Question

This topic is 3341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Question: How would I determine if a "key" exists in a map? Background: Im making an image manager that has a single function that takes a string paramater that is the file folder/file name of the image file. Example:
Image& Ret(string FileName)


I want it to return a reference to the image in memory. This function will have a dual function as "getter" and "setter." The map is <FileName, Image> so the keys will be the FileName. How would I go about determining if map[FileName] exists? I tried using find() but it returns an iterator and I tried evaluating it as NULL but it didnt work, so im kind of lost as to how I would check it or if there is a better way. Thank you for the help :) Edit: Also, is it possible to use strings with enums? Example:
enum Files
{
ORC = ".../.../data/Orc.png",
DWARF = ".../.../data/Dwarf.png"
};



Share on other sites
I believe you check whether 'map.find(key) != map.end()'. If it is equal to map.end(), then it is not in the map.

Share on other sites
Quote:
 Original post by UphoreumI believe you check whether 'map.find(key) != map.end()'. If it is equal to map.end(), then it is not in the map.

I thought about that, but what if the key is literally the last key in the map? Then wouldnt that be map.end() ?

Share on other sites
I guess end() is not inclusive? I don't know, that's how I've always done it and I don't think it's ever caused an issue.

EDIT: And no, I'm pretty sure in C++ enum's are always integer values.

Share on other sites
end() does not return the last in the map. What if there was nothing in the map? What would it return then?

Share on other sites
Indeed, you check if the iterator is
equal to the end. See here for example.

Enums are for integer values only.

Share on other sites
Thank you all for the quick replies! I will give end() a try. As far as my 2nd question goes... how do you think I can achieve something like that?

I want to be able to create something like a string enum at the beginning of my game or for each game state, and then pass in those values to my manager.

Taking the example enum I had above I could rewrite my ImageManager function as such:
Image& Ret(Files FileName)

and then using it would be something like this

Im::Ret(Files::Orc);

Any ideas or tips let me know. Thanks for the help again!

Share on other sites
Uphoreum is correct, testing against map.end() is what you want. end() returns the location immediately following the last item in your map.

Edit: ninja'd (but without the cool sound effects from the old hong kong kung-fu movies)

Edit 2: You could always try something along these lines:
#include <string>#include <iostream>namespace Strings{	std::string const One = "one";	std::string const Two = "two";	std::string const Three = "three";}void SomeFunction(std::string const& s){	std::cout << s << "\n";	return;}int main(int argc, char** argv){	SomeFunction(Strings::One);	SomeFunction(Strings::Two);	SomeFunction(Strings::Three);	return 0;}

Share on other sites
Well, I suppose you could do something like:

class Files{public:    static std::string Orc = "Orc.png";};

I don't know if this is frowned upon for some reason though.

Share on other sites
Quote:
 Original post by UphoreumI don't know if this is frowned upon for some reason though.

The fact that it won't compile would be a pretty big reason.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633768
• Total Posts
3013744
×