std::map Question

Started by
9 comments, last by Uphoreum 14 years, 5 months ago
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"
};

__________________________________________
Eugene Alfonso
GTP | Twitter | SFML | OS-Dev
Advertisement
I believe you check whether 'map.find(key) != map.end()'. If it is equal to map.end(), then it is not in the map.
Quote:Original post by Uphoreum
I 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() ?
__________________________________________
Eugene Alfonso
GTP | Twitter | SFML | OS-Dev
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.
end() does not return the last in the map. What if there was nothing in the map? What would it return then?
Indeed, you check if the iterator is
equal to the end. See here for example.

Enums are for integer values only.
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!
__________________________________________
Eugene Alfonso
GTP | Twitter | SFML | OS-Dev
Uphoreum is correct, testing against map.end() is what you want. end() returns the location immediately following the last item in your map.

To answer your other question, enums can only be integer values.

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;}
Humble people don't refer to themselves as humble (W.R.T. "IMHO".)
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.
Quote:Original post by Uphoreum
I 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.

This topic is closed to new replies.

Advertisement