0

I have a map that contains a string to sprite association. This function is used to return the sprite to draw on the screen. Here is the function:

sf::Sprite LoopSprite()
{
    for (std::map<std::string,sf::Sprite>::iterator it=SpriteMap.begin(); it!=SpriteMap.end(); ++it)
    {
       return it->second;
    }
}

I used to have these in a vector which made loops for drawing easy, but I wanted to use a map to allow easier recognition for maintaining code. The function above only allows one image from the map to be drawn. Is there something wrong with this function?

sf::Sprite LoopSprite(int element)
{
    return vec[element];
}

As pointed this works because it takes an element so there is only one return, but I want the same result except with the map. The issue is that it is easy to send in a 0-vec.size. My only idea is to have a vector of strings to allow easy looping.

6
  • How are you attempting to use this function? It will simply return the first sf::Sprite on the first iteration. It wouldn't work with vectors either. Apr 5, 2013 at 18:31
  • You are returning in the first iteration itself!
    – Naveen
    Apr 5, 2013 at 18:32
  • In other words, your function does the same as: sf::Sprite LoopSprite() { return SpriteMap.begin()->second; }
    – jrok
    Apr 5, 2013 at 18:34
  • That's what I was worried about. The vector function took in an int to return an individual element and the loop was outside. I am thinking about trying to approach this with a vector of strings heh
    – Chivos
    Apr 5, 2013 at 18:38
  • I have a hard time trying to figure out what you want to do. Can you show the vector equivalent that you want to replace with map?
    – W.B.
    Apr 5, 2013 at 18:43

2 Answers 2

1

Your function will simply return in the first iteration, returning only the first element in the map.

You say in the comments that originally, when you were using a vector, the function took an int argument and returned that element. If you still want the function, you can achieve the same with a map like so:

sf::Sprite LoopSprite(std::string key)
{
    return SpriteMap[key];
}

If you want to iterate over the elements of the map inside LoopSprite, then you'll need to move the lines of code that actually do something with each element into the function:

void LoopSprite()
{
    for (std::map<std::string,sf::Sprite>::iterator it=SpriteMap.begin(); it!=SpriteMap.end(); ++it)
    {
       // Do something with it->second
    }
}
0
1

First of all you are returning local variable which is not allowed. Even after returning from the function it may be null or invalid.

You are only returning this part SpriteMap.begin() which is actually useless. This code is much easier.

sf::Sprite LoopSprite(string key)
{
    std::map<std::string,sf::Sprite>::iterator it = SpriteMap.begin();

    while(it!=SpriteMap.end())
    {
       if(check) // check if this is your desired key
           return value;
       ++it;
    }
    return null;
}

map is used usually in a simple way:

string key = "keyItem";    
sf::Sprite ans = SpriteMap[key];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.