Map with vectors in it

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

Recommended Posts

Hey all, I've never used maps before and I'm trying to make a map that holds an int/vector combo. All the tutorials I can find are a bit confusing to me. This line is causing an error and I can't figure out why. animations.insert(pair<int, vector<SDL_Rect> >(direction, frames)); I'm not even sure what pair is... I googled it and read some on it but didn't quite understand what it did. Can anyone point me in the right direction? Error is: Line Location stl_pair.h:90: error: no matching function for call to 'std::vector<int, std::allocator<int> >::vector(const std::vector<SDL_Rect, std::allocator<SDL_Rect> >&)' What I'm doing is trying to come up with a way to handle sprite sheets. I was thinking I'd use a map where the first value could be enumerated directions such as up, down, left, right, standing, etc, and the value that would go with it would be a vector of frames pertaining to the direction the sprite will be moving that could be looped through as needed. Is this even a good idea?

Share on other sites
What are the types of everything involved?

Share on other sites
direction is an enumeration (int, right?) and frames is a vector<SDL_Rect>.

And animations?

Share on other sites
Oh, sorry. It's a map.

map<int, vector<int> > animations;

That's how it's declared, right?

Share on other sites
Why are you trying to put a vector<SDL_Rect> into a map of vector<int>?

Share on other sites
Very good question. I can't believe I didn't notice that. Well it's compiling now so hoping that fixed it. Thanks!

Share on other sites
Quote:
 Original post by JaqenI'm not even sure what pair is
It's one of the simplest complex types you'll ever meet: a tuple of size 2. Basically it holds two (a pair) of values called first and second (the names of the member variables), the types of the two variables are those types that you used as the template arguments.

Quote:
 Is this even a good idea?
It's not a bad idea, naturally I don't know enough about your application to know whether it's in any way optimal.

Quote:
 direction is an enumeration (int, right?) and frames is a vector.

Well an enum type is not equivalent to the int type, using an int as the map key will work though because by default an enum is implicitly convertible to an int.

Your code should look something like this:

enum directions { up, down, left, right };std::map<directions, std::vector<SDL_Rect> > animations;animations.insert(std::make_pair(direction, frames));// it's easier to use make_pair rather than create a pair manually, although// this is possible too:// animations.insert(std::pair<directions, std::vector<SDL_Rect> >(direction, frames));

It's worth looking into using typedef to create aliases for difficult or repetitively used type names:

enum directions { up, down, left, right };typedef std::vector<SDL_Rect> frame_seq;typedef std::map<directions, frame_seq> animation_map;typedef animation_map::value_type dir_anim_pair; // n.b. the value_type of a map is the pair type you need.animation_map animations;animations.insert(dir_anim_pair(direction, frames));

Share on other sites
Thanks. All of that helps a lot. I do have a few more questions now, though.

I have my enumeration with directions (left, right, up, down) in my sprite class. If I wanted to call those from main(), how would I do that? Should I move the enumeration from the class and make it a global instead?

Also I'm having trouble figuring out how to pull data from the map. I did:

mySprite.draw(5, 5, myEngine.getScreen(), &mySprite.getFrame(1, 0));
(this is also where I have a problem with not knowing the enumeration thing... I'd like to be able to do mySprite.getFrame(right, 1).

And it's working where getFrame is returning the map values. But when I do stuff like this I get a warning saying I'm taking the address of temporary. How do I not do this?

Share on other sites
Quote:
 Original post by JaqenI have my enumeration with directions (left, right, up, down) in my sprite class. If I wanted to call those from main(), how would I do that? Should I move the enumeration from the class and make it a global instead?

Just use the scope resolution operator as normal: refer to the enum as Sprite::directions rather than just directions.

Quote:
 And it's working where getFrame is returning the map values. But when I do stuff like this I get a warning saying I'm taking the address of temporary. How do I not do this?

Have Sprite::getFrame return a reference? And perhaps think over whether Sprite::draw should be taking that parameter as a pointer or something else (depending on what draw does and what a 'frame' actually is).

For example, could getFrame still return a copy (should it be making a copy or does it make sense to modify the value it returns, in which case perhaps a reference is more appropriate?) and draw take that parameter by const reference?

Share on other sites
Quote:
 Original post by JaqenVery good question. I can't believe I didn't notice that. Well it's compiling now so hoping that fixed it. Thanks!

Quote:
 Line Location stl_pair.h:90: error: no matching function for call to 'std::vector >::vector(const std::vector >&)'

If we mentally cut out the std:: prefixes here, and the allocator template parameters, we can read this as:

no matching function for call to 'vector<int>::vector(const vector<SDL_Rect>&)'

Now it's more obvious, hmm? :)

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628644
• Total Posts
2984012

• 9
• 9
• 10
• 21
• 20