Suppose I want to use some object's member function as the action
passed to a for_each call:
class A {
...
public:
void foo(int );
};
A a;
int ints[5];
....
for_each(0, 5, /*i want to call a.foo for each element in the range*/);
What is a good elegant way to do that?
What I did was to wrap the a object into a unary_function-derived
object and then pass the unary_function to the for_each method.
Any other ideas?
Thanks 6 4036
Belebele schrieb:
Suppose I want to use some object's member function as the action
passed to a for_each call:
class A {
...
public:
void foo(int );
};
A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the range*/);
What is a good elegant way to do that?
#include <algorithm>
#include <boost/bind.hpp>
class A {
public:
void foo(int i) { };
};
int main()
{
A a;
int ints[5] = { 1, 2, 3, 4, 5};
std::for_each(i nts, ints+5, boost::bind(&A: :foo, a, _1));
}
There may be a way using std::bind* functions.
--
Thomas
Belebele wrote:
Suppose I want to use some object's member function as the action
passed to a for_each call:
class A {
...
public:
void foo(int );
};
A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the range*/);
What is a good elegant way to do that?
What I did was to wrap the a object into a unary_function-derived
object and then pass the unary_function to the for_each method.
Any other ideas?
Thanks
The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :
for_each(0, 5, std::bind1st(st d::mem_fun(A::f oo), a));
Another option is to use the boost::bind library, in which case it would
look like:
for_each(0, 5, bind(A::foo, a, _1));
Pierre
Thomas J. Gritzan wrote:
Belebele schrieb:
>Suppose I want to use some object's member function as the action passed to a for_each call:
class A { ... public: void foo(int ); };
A a; int ints[5]; ... for_each(0, 5, /*i want to call a.foo for each element in the range*/);
What is a good elegant way to do that?
#include <algorithm>
#include <boost/bind.hpp>
class A {
public:
void foo(int i) { };
};
int main()
{
A a;
int ints[5] = { 1, 2, 3, 4, 5};
std::for_each(i nts, ints+5, boost::bind(&A: :foo, a, _1));
}
There may be a way using std::bind* functions.
std::for_each(i nts, ints+5,
std::bind1st(st d::mem_fun1(&A: :foo), &a));
(include <functionalinst ead of <boost..>)
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Pierre Barbier de Reuille wrote:
[..]
The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :
for_each(0, 5, std::bind1st(st d::mem_fun(A::f oo), a));
Try it before recommending it. It won't work. Binders are tricky.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
class A {
...
public:
void foo(int );
};
The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :
for_each(0, 5, std::bind1st(st d::mem_fun(A::f oo), a));
So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.
Thanks!
Belebele wrote:
>class A { ... public: void foo(int ); };
The best is to use std::mem_fun ... it already does what you want ! However, you need to combine it with std::bind1st like that :
for_each(0, 5, std::bind1st(st d::mem_fun(A::f oo), a));
So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.
Thanks!
bind1st doesn't do anything like that. It just binds the first argument
of the function. It is std::mem_fun that convert a member function into
an object whose first argument is the object needed for the member
function. Also, as suggested higher, the for_each won't work like that,
you need an iterator (sorry, I copied that a bit quickly), so it should be:
int ints[6] = {0,1,2,3,4,5};
for_each(ints, ints+6, std::bind1st(st d::mem_fun(&A:: foo), &a));
(I also forgot the references ...)
Pierre This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christof Krueger |
last post by:
Hello,
I'm quite new to C++ so maybe there's something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the board. Since
this function does not change anything in the class I declared it as
const. To count all pieces of a given color the functions iterates
through a "map" of CNode-pointers. "CNode" is another class that is
irrelevant to the problem.
|
by: flyaflya |
last post by:
i use a class static member function to for_each:
class t1
{
static test(string s);
}
main()
{
list<string> slist;
...
|
by: pookiebearbottom |
last post by:
Ok, this is what I am trying to do. I have a
member function that as a parameter accepts a
pointer to a different member function. In this 2nd
function, I want to iterate over an STL container
and call the passed in member function. I can do
this in my own loop, but I am looking to call
for_each. I am a little confused on the syntax to
use. Any help would be appreciated.
look at the following code and at the comment in
|
by: Grasshopper |
last post by:
Hi,
I am automating Access reports to PDF using PDF Writer 6.0. I've
created a DTS package to run the reports and schedule a job to run this
DTS package. If I PC Anywhere into the server on where the job is
running, the job runs sucessfully, PDF files got generated, everything
is good. If I scheduled the job to run at the time that I am not
logged into the server, Access is not able to print to the printer.
The error is pretty...
|
by: Belebele |
last post by:
I have an Element class which is abstract and I would like to have an
object of the Iterator class to iterate over a range of elements.
I would like to use std::for_each to instrument the iteration, which
forces me to have certain interface on the Iterator class.
struct Element {
virtual ~Element() = 0;
};
| |
by: Sarath |
last post by:
What's the advantage of using for_each than the normal iteration using
for loop?
Is there any 'tweak' to use stream objects as 'Function' parameter of
for_each loop. Seems copy function can do the same.
|
by: Yan |
last post by:
Hi,
I don't seem to be able to use for_each if it should replace a 'for'
loop in a method (constructor in my case) and inside that 'for' loop a
class member variable is being accessed. The presence of this member
variable prevents me from using a static or global method to be passed
as a third parameter to for_each, and mem_fun doesn't seem to work for
me either as I am not going to execute a method of an iterator but
pass an iterator...
|
by: want.to.be.professer |
last post by:
We know that alomost every algorithm function, such as for_each,
find_if, use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
|
by: church7 |
last post by:
Hello,
I wrote the following class:
In this case, the error message was
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |