c++ - Issues with returning map loop -


i have map contains string sprite association. function used return sprite draw on screen. here function:

sf::sprite loopsprite() {     (std::map<std::string,sf::sprite>::iterator it=spritemap.begin(); it!=spritemap.end(); ++it)     {        return it->second;     } } 

i used have these in vector made loops drawing easy, wanted use map allow easier recognition maintaining code. function above allows 1 image map drawn. there wrong function?

sf::sprite loopsprite(int element) {     return vec[element]; } 

as pointed works because takes element there 1 return, want same result except map. issue is easy send in 0-vec.size. idea have vector of strings allow easy looping.

your function return in first iteration, returning first element in map.

you in comments originally, when using vector, function took int argument , returned element. if still want function, can achieve same map so:

sf::sprite loopsprite(std::string key) {     return spritemap[key]; } 

if want iterate on elements of map inside loopsprite, you'll need move lines of code each element function:

void loopsprite() {     (std::map<std::string,sf::sprite>::iterator it=spritemap.begin(); it!=spritemap.end(); ++it)     {        // it->second     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -