473,480 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::for_each question

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
Jul 22 '05 #1
5 4333

"Dylan" <sp******@ontheball.com> skrev i meddelandet
news:iq********************************@4ax.com...
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
Jul 22 '05 #2
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.
Jul 22 '05 #3
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)
Jul 22 '05 #4
jota wrote:

"Dylan" <sp******@ontheball.com> skrev i meddelandet
news:iq********************************@4ax.com...
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)
Jul 22 '05 #5
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2591
by: sashan | last post by:
Sometimes I write code using std::for_each. Sometimes I use it on several container classes that are the same size. for_each(v.begin(), v.end(), f1()); for_each(u.begin(), u.end(), f2()); ...
2
2028
by: psujkov | last post by:
Hi everybody, does anyone knows is there any way to have an implicit iteration counter in std::for_each ? e.g. std::for_each(c.begin(), c.end(), boost::bind(&C::set_id, _1,...
22
6364
by: yurec | last post by:
Hi I wanna give template method of library class with some predicate, which I put to std::for_each in that method.How to break the std::for_each with predicate?(exceptions are used olny in...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.