currently i'm having problem with my code. I tried everything. image is here I hope someone can help me :). Btw i'm new to c++
-
Give [this](http://stackoverflow.com/questions/110157/how-to-retrieve-all-keys-or-values-from-a-stdmap) a look – pie3636 Jul 30 '16 at 16:09
-
Can you tell me what is wrong in my code? – Petar Putar Visnjic Jul 30 '16 at 16:11
-
You are iterating over sprites.comp_value, which is a comparison function for the sprites in your map, and doesn't make sense in this context. I suggest trying: `for (auto sprite : sprites)` which will iterate over the keys. You can then access the values using `sprite.second`. This is assuming you're using the STL map container. – pie3636 Jul 30 '16 at 16:13
-
2@Petar Putar Visnjic Could you please add your code as text in your question, this way it would be visible immediately and also be searchable. Also, please state what the problem is. – Peter G. Jul 30 '16 at 16:18
-
@PeterG. From the title, the OP is trying to get a collection (a `vector` here) containing all the sprites in their `sprites` collection, which seems to be a set or a map. – pie3636 Jul 30 '16 at 16:31
2 Answers
#include <SFML\Graphics.hpp>
std::map<std::string, sf::Sprite> sprites;
void addSprite(std::string sprite_name, sf::Texture sprite_texture) {
sf::Sprite sprite;
sprite.setTexture(sprite_texture);
sprites[sprite_name] = sprite;
}
sf::Sprite getSprite(std::string sprite_name) {
return sprites[sprite_name];
}
std::vector<sf::Sprite> getSprites() {
std::vector<sf::Sprite> sprites_c2;
for each (sf::Sprite sprite in sprites.value_comp) {
sprites_c2.emplace_back(sprite);
}
return sprites_c2;
}

- 5,266
- 23
- 39
- 56

- 27
- 5
-
You might want to edit your original post next time; this section is meant for people trying to answer your question. – pie3636 Jul 30 '16 at 16:40
for each
is not valid C++; starting with C++11, the correct expression is
for (<type> <variable> : <collection>)
Here, you are also trying to access the collection of elements through sprites.value_comp
, which is a function and doesn't make sense here.
Assuming sprites
is a std::set
container, you can iterate over it this way:
for (auto sprite : sprites) {
// sprite is the value you're looking for
}
If you are using a std::map
instead, use the same loop, but sprite
will not contain your sf::Sprite
but its key, and you will need to use sprite.second
to access the sprite.
Note that the auto
keyword lets the compiler determine the type of the variable.
If your compiler does not support C++11, you will need to do something like this instead:
std::set<sf::Sprite>::iterator it;
for (it = sprites.begin(); it != sprites.end(); ++it)
{
// Access each sprite using *it (the star is necessary here)
}
With a std::map
:
// Replace "keyType" with the type of the keys, such as int or std::string
std::map<keyType, sf::Sprite>::iterator it; for (it = sprites.begin(); it != sprites.end(); ++it)
{
// Access each sprite using it->second
}
If you are just trying to convert your std::set
into a std::vector
, however, there are easier ways to do that:
std::vector <sf::Sprite> sprites_c2;
std::copy(sprites.begin(), sprites.end(), std::back_inserter(sprites_c2));
For a std::map
there aren't any single-line versions, so you will have to iterate with a loop as I stated above.

- 795
- 17
- 31
-
Note that there is an even simpler way, assuming you're just trying to convert a `std::set` into a `std::vector`: just create an empty vector and add `std::copy(sprites.begin(), sprites.end(), std::back_inserter(sprites_c2)); ` – pie3636 Jul 30 '16 at 16:33