std::map

Get answers to all your basic programming questions. No Ogre questions, please!
ludoz
Gnoblar
Posts: 17
Joined: Sun Jan 16, 2005 8:00 pm
Location: italy

std::map

Post by ludoz »

typedef std::map<String, AnimationState> AnimationStateSet;

What's that ?? what return that ? how can i access animationState object from animationstateset ?
User avatar
:wumpus:
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3067
Joined: Tue Feb 10, 2004 12:53 pm
Location: The Netherlands
x 1

Post by :wumpus: »

http://www.sgi.com/tech/stl/Map.html

Nuff said, it is a dictionary that can look up AnimationState objects by name (String).

You can use the find method to look up a key in a Map, like

Code: Select all

AnimationStateSet::iterator found = set.find("SwimSwimHungry");
if(found != set.end())
{
    // The animation state has been found, do something with it
}
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8

Post by haffax »

Welcome to the complex world of the STL. ;) A very good STL reference can be found here.

A std::map is set of key-value pairs. So in the case of your map the key type is String and the value type is AnimationState. There are several ways to access elements in the container.

One way:

Code: Select all

AnimationState state = myAnimationStateSet["walkloop"];
This is the easiest way, but there are a few properties that you must be aware of. Look into the dokumentation for details.

Another way is through iterators:

Code: Select all

// set iterator to first element in map
AnimationStateSet::iterator it = myAnimationStateSet.begin();
// while loop for all elements in the map.
while(it != myAnimationStateSet.end())
{
    String key = it->first;
    String state = it->second;
    //do something with them
    .....
    ++it; // set iterator to the next element
}
You can't possibly learn STL in one day. You should look out for a tutorial or some such.
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
:wumpus:
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3067
Joined: Tue Feb 10, 2004 12:53 pm
Location: The Netherlands
x 1

Post by :wumpus: »

tanis wrote:

Code: Select all

AnimationState state = myAnimationStateSet["walkloop"];
Don't do this! Use find(). It generates more code than find() as it creates "walkloop" in cases it did not exist yet (initializing it to default values), which is usually not what you want anyway. This is one of the traps that makes STL outright dangerous to people new to it. Only use the [] syntax to set values:

Code: Select all

myAnimationStateSet["walkloop"] = AnimationState(param, param2, ...);