Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::for_each question

Reply
Thread Tools

std::for_each question

 
 
Dylan
Guest
Posts: n/a
 
      07-04-2004
At the moment I often find myself doing something like the following

std::list<MyClass*> mcl;

/.../fill mcl with some elements

//now call aMethod() for each element;
for (std::list<MyClass*>::iterator it = mcl.begin();
it != mcl.end();
++it)
{
it->aMethod();
}

Is there a *convenient* way of using something like std::for_each to
achieve the same result as the loop shown in the previous code
snippet?

thanks
 
Reply With Quote
 
 
 
 
jota
Guest
Posts: n/a
 
      07-04-2004

"Dylan" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed)...
> At the moment I often find myself doing something like the following
>
> std::list<MyClass*> mcl;


std::for_each(mcl.begin(),mcl.end(),aMethod);
//jota


 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      07-04-2004
Dylan wrote:
> At the moment I often find myself doing something like the following
>
> std::list<MyClass*> mcl;
>
> /.../fill mcl with some elements
>
> //now call aMethod() for each element;
> for (std::list<MyClass*>::iterator it = mcl.begin();
> it != mcl.end();
> ++it)
> {
> it->aMethod();
> }
>
> Is there a *convenient* way of using something like std::for_each to
> achieve the same result as the loop shown in the previous code
> snippet?


std::for_each (mcl.begin (), mcl.end (),
std::mem_fun (& MyClass::aMethod));

--
Regards,
Buster.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      07-04-2004
Dylan wrote:
>
> Is there a *convenient* way of using something like std::for_each to
> achieve the same result as the loop shown in the previous code
> snippet?


Read about std::mem_fun.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      07-04-2004
jota wrote:
>
> "Dylan" <(E-Mail Removed)> skrev i meddelandet
> news:(E-Mail Removed)...
> > At the moment I often find myself doing something like the following
> >
> > std::list<MyClass*> mcl;

>
> std::for_each(mcl.begin(),mcl.end(),aMethod);


Not quite. This would attempt to call aMethod(*iter), which is fine if
aMethod is an ordinary function, but what you need with a member
function is (*iter)->aMethod. Use

std::for_each(mc1.begin(), mc1.end(), mem_fun(&MyClass::aMethod));

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
=?iso-8859-1?q?Josep_Mon=E9s_i_Teixidor?=
Guest
Posts: n/a
 
      07-05-2004
On Sun, 04 Jul 2004 20:41:05 +0100, Dylan wrote:

> At the moment I often find myself doing something like the following
>
> std::list<MyClass*> mcl;
>
> /.../fill mcl with some elements
>
> //now call aMethod() for each element;
> for (std::list<MyClass*>::iterator it = mcl.begin();
> it != mcl.end();
> ++it)
> {
> it->aMethod();
> }
>
> Is there a *convenient* way of using something like std::for_each to
> achieve the same result as the loop shown in the previous code
> snippet?


I think you could use something like:

std::for_each(mcl.begin(), mcl.end(), std::mem_fun(&MyClass::aMethod))

If you want it to be even more powerful, try Boost. For instance, look at
this sample:
http://www.boost.org/libs/bind/bind....ith_algorithms

Regards,

Josep


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments