iterate through this

Started by
10 comments, last by MTclip 18 years, 4 months ago
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 ...
Advertisement
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) {        // ...    }}
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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
}
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.
... 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 specificiterator = rAgentMap.find(m_pStats->m_agentType);if(iterator != rAgentMap.end()){	for (agentIterator = iterator->second.begin(); agentIterator != iterator->second.end(); ++agentIterator)	{        // stuff	}}else{// other stuff}
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 ;(
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.
... we need 'auto' so bad ....
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?

This topic is closed to new replies.

Advertisement