C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to use for_each on map?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
true_smart@hotmail.com
Guest





PostPosted: Tue Jan 25, 2005 10:43 am    Post subject: How to use for_each on map? Reply with quote



I want use "for_each" to relpace the "for", following are my test code
segment.
class MapTestter
{
private:
long m_nTest;

public:
MapTestter(long n)
{
m_nTest = n;
}
~MapTestter()
{
int s = 0;
s++;
}
void print(int i)
{
std::cout< }
};

map
// I want to use "for_each" to relpace the "for"
for (long i = 0; i < 10; i++)
{
MapTestter *pNew = new MapTestter(i);
test.insert(make_pair(i, pNew));
}

// I want to use "for_each" to relpace the "for"
for (map test.end(); iter++)
{
(*iter).second->print(3);
}

for (map<long, MapTestter*>::iterator iter = test.begin(); iter !=
test.end(); iter++)
{
delete ((*iter).second);
}
How can I use "for_each"?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Allan W
Guest





PostPosted: Tue Jan 25, 2005 8:12 pm    Post subject: Re: How to use for_each on map? Reply with quote



/**/ #include <iostream>
/**/ #include <map>
/**/ #include <algorithm>

/**/ //-- Here's your original MapTestter class
/**/ //-- (Misspelled? Try MapTester, no double-t)
/**/ class MapTestter {
/**/ private:
/**/ long m_nTest;
/**/ public:
/**/ MapTestter(long n) : m_nTest(n) {}
/**/ //-- Your destructor did nothing, so I removed it
/**/ void print(int i) { std::cout< /**/ };

/**/ //-- Here's your original map
/**/ std::map
/**/ //-- Here's a class I added that calls the print function for you.
/**/ class MapTestterPrinter {
/**/ int arg;
/**/ public:
/**/ MapTestterPrinter(int n) : arg(n) {}
/**/ void operator()(std::map<long, MapTestter*>::value_type m)
/**/ { m.second->print(arg); }
/**/ };

/**/ //-- Here's another one that calls delete for you
/**/ struct MapTestterDeleter {
/**/ void operator()(std::map<long, MapTestter*>::value_type m)
/**/ { delete m.second; }
/**/ };

/**/ int main() {
/**/ // I want to use "for_each" to relpace the "for"
/**/ for (long i = 0; i < 10; i++) {
/**/ MapTestter *pNew = new MapTestter(i);
/**/ test.insert(std::make_pair(i, pNew));
/**/ }
/**/ //-- That's not going to happen.
/**/ //-- So far, test is empty...
/**/ //-- Any use of for_each is going to result in 0 iterations

/**/ // I want to use "for_each" to relpace the "for"
/* /*-- This time, it IS possible.
/* for (std::map /* iter != test.end(); iter++)
/* {
/* (*iter).second->print(3);
/* } /*/
/**/ //-- Use a Functor class, MapTestterPrinter
/**/ MapTestterPrinter functor(3);
/**/ std::for_each(test.begin(), test.end(), functor);

/**/ /*-- Can do much the same thing here
/* for (std::map<long, MapTestter*>::iterator iter = test.begin();
/* iter != test.end(); iter++)
/* {
/* delete ((*iter).second);
/* } /*/
/**/ std::for_each(test.begin(), test.end(), MapTestterDeleter());
/**/ return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
alexmdac@hotmail.com
Guest





PostPosted: Wed Jan 26, 2005 10:06 am    Post subject: Re: How to use for_each on map? Reply with quote



std::map< A, B > theMap;

inline
void fn( std::pair< A, B > &mapEntry )
{
// do something here
}

// ...

std::for_each( theMap.begin(), theMap.end(), fn );


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.