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 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
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.
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)
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)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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());
...
|
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,...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |