Jump to content
  • Advertisement
Sign in to follow this  
phear-

std::map Question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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() ?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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;
}

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!