Sign in to follow this  
MTclip

iterate through this

Recommended Posts

MTclip    242
I just started using iterators and this one has got me good ;P std::map<std::string, std::vector<Agent*> > I would like to loop through like this ///////////////// for each string for each Agent in vector associated with current string ///////////////// i would post what i have so far but I think it just so wrong it would confuse people ...

Share this post


Link to post
Share on other sites
Roboguy    794
You probably want some thing like (been a while since I've used STL, so this may not be exactly how it is supposed to be):

std::map<std::string, std::vector<Agent*> > map;
for (std::map<std::string, std::vector<Agent*> >::iterator iterator = map.begin(); iterator != map.end(); ++iterator) {
for (std::vector<Agent*>::iterator agentIterator = (*iterator).begin(); agentIterator != (*iterator).end(); ++agentIterator) {
// ...
}
}


Share this post


Link to post
Share on other sites
iMalc    2466
My STL isn't brilliant but...
typedef std::vector<Agent*> agentvector;
typedef std::map<std::string, agentvector> mymap;
mymap agentmap;

for (mymap::iterator it = agentmap.begin(); it != agentmap.end(); ++it)
{
agentvector &agents = it->second;
for (agentvector::iterator vit = agents.begin(); vit != agents.end(); ++vit)
{
Agent *a = *vit;
// Now do whatever with a
}
}

Of course you should use const_iterators if you're not going to modify anything.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Do you really want to iterate through ALL existing strings? Or just those that are keys in your map? If so try this:

typedef std::map > tAgentMap;

tAgentMap mapMap

for( tAgentMap::iterator i = mapMap.begin();
i != mapMap.end()
++i )
{
// blablabla
}

Share this post


Link to post
Share on other sites
jflanglois    1020
Example:
#include <vector>
#include <map>
#include <string>

class Agent {};

typedef std::vector< Agent * > AgentVector;
typedef std::map< std::string, AgentVector > AgentMap;
typedef AgentVector::iterator AgentVectorIterator;
typedef AgentMap::iterator AgentMapIterator;

int main() {
AgentMap agents;

AgentVector one( 10, ( Agent * )0 );
AgentVector two( 3, ( Agent * )1 );

agents["one"] = one;
agents["two"] = two;

for( AgentMapIterator mi = agents.begin(); mi != agents.end(); ++mi ) {
for( AgentVectorIterator vi = mi->second.begin(); vi != mi->second.end(); ++ vi ) {
// Do Stuff
}
}
}



Explanation:
Do note that I have a dummy Agent class.
typedef std::vector< Agent * > AgentVector;
typedef std::map< std::string, AgentVector > AgentMap;
typedef AgentVector::iterator AgentVectorIterator;
typedef AgentMap::iterator AgentMapIterator;
typedef std::pair< std::string, AgentVector > MapPair;
Typedefs to make it easier to read later on.


for( AgentMapIterator mi = agents.begin(); mi != agents.end(); ++mi )
Iterate through the map with iterator mi


for( AgentVectorIterator vi = mi->second.begin(); vi != mi->second.end(); ++ vi )
*mi is actually a std::pair< std::string, std::vector< Agent * > >. So you want to iterate through the vector that is pointed to by the second member of the pair.



jfl.

[edit] Oh well, so much for being quick.

Share this post


Link to post
Share on other sites
MTclip    242
... thanks guys...

i got it to work and in the end it look something like this


std::map<std::string, std::vector<Agent*> >::iterator iterator;
std::vector<Agent*>::iterator agentIterator;

// FIND TYPE <--- will be several case specific
iterator = rAgentMap.find(m_pStats->m_agentType);
if(iterator != rAgentMap.end())
{
for (agentIterator = iterator->second.begin(); agentIterator != iterator->second.end(); ++agentIterator)
{
// stuff
}
}
else
{
// other stuff
}

Share this post


Link to post
Share on other sites
MTclip    242
dang... i spoke a little to soon...


from the above this would be in the inner most loop..
//
//testDist = D3DXVec3Length(&m_state.GetPos() - agentIterator->m_state.GetPos());
//
that causes an error.. how to i convert the iterator to an Agent... my intellisense is broken ;(

Share this post


Link to post
Share on other sites
jflanglois    1020
You would probably do *agentIterator->m_state.GetPos().
The reason for this is that when calling operator-> on an iterator, you dereference the iterator, but what you get is an Agent *, so you need to dereference twice.

jfl.

Share this post


Link to post
Share on other sites
Roboguy    794
Quote:
Original post by Anonymous Poster
... we need 'auto' so bad ....


C++ already has a auto keyword, which seems pretty useless. I assume you mean type inference?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this