Can for_each() be used with multi-argument functions? 
Author Message
 Can for_each() be used with multi-argument functions?

If so, how do you do it?


Sun, 11 Dec 2005 07:20:01 GMT  
 Can for_each() be used with multi-argument functions?

Quote:
> If so, how do you do it?

It depends on what exactly you're asking.  Yes, for_each can be used with
multi-argument functions, given certain conditions and caveats.

What exactly do you want to pass to this multi-argument function?  Tell us
that and we'll tell you whether you can do it or what your alternatives are.

Ken



Sun, 11 Dec 2005 13:32:19 GMT  
 Can for_each() be used with multi-argument functions?
I'm using lists of object pointers, and I have a lot of functions like this:

VOID
ProcessList(LISTTYPE &theList, TYPE1 arg1, TYPE2 arg2)
{
    for (LISTTYPE::iterator p = theList.begin(); p != theList.end(); ++p)
        if ((*p)->GetID() == arg1)
        {
            (*p)->DoSomething(arg2);
        }

Quote:
}

Can I accomplish this with for_each()?


Quote:


> > If so, how do you do it?

> It depends on what exactly you're asking.  Yes, for_each can be used with
> multi-argument functions, given certain conditions and caveats.

> What exactly do you want to pass to this multi-argument function?  Tell us
> that and we'll tell you whether you can do it or what your alternatives
are.

> Ken



Sun, 11 Dec 2005 23:42:36 GMT  
 Can for_each() be used with multi-argument functions?

Quote:
> I'm using lists of object pointers, and I have a lot of functions like
this:

> VOID
> ProcessList(LISTTYPE &theList, TYPE1 arg1, TYPE2 arg2)
> {
>     for (LISTTYPE::iterator p = theList.begin(); p != theList.end();
++p)
>         if ((*p)->GetID() == arg1)
>         {
>             (*p)->DoSomething(arg2);
>         }
> }

> Can I accomplish this with for_each()?

struct ListProcessor
{
    ListProcessor(TYPE1& arg1, TYPE2& arg2) : m_arg1(arg1), m_arg2(arg2)
{}
    void operator()(MyObject* p) const
    {
        if (p->GetID() == m_arg1)
        {
            p->DoSomething(m_arg2);
        }
    }
    TYPE1& m_arg1;
    TYPE2& m_arg2;

Quote:
};

for_each(theList.begin(), theList.end(), ListProcessor(arg1, arg2) );

--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Sun, 11 Dec 2005 23:55:03 GMT  
 Can for_each() be used with multi-argument functions?
Thank you.


Quote:


> > I'm using lists of object pointers, and I have a lot of functions like
> this:

> > VOID
> > ProcessList(LISTTYPE &theList, TYPE1 arg1, TYPE2 arg2)
> > {
> >     for (LISTTYPE::iterator p = theList.begin(); p != theList.end();
> ++p)
> >         if ((*p)->GetID() == arg1)
> >         {
> >             (*p)->DoSomething(arg2);
> >         }
> > }

> > Can I accomplish this with for_each()?

> struct ListProcessor
> {
>     ListProcessor(TYPE1& arg1, TYPE2& arg2) : m_arg1(arg1), m_arg2(arg2)
> {}
>     void operator()(MyObject* p) const
>     {
>         if (p->GetID() == m_arg1)
>         {
>             p->DoSomething(m_arg2);
>         }
>     }
>     TYPE1& m_arg1;
>     TYPE2& m_arg2;
> };

> for_each(theList.begin(), theList.end(), ListProcessor(arg1, arg2) );

> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken



Mon, 12 Dec 2005 03:56:47 GMT  
 Can for_each() be used with multi-argument functions?
I have a member function that takes three parameters: a pointer to an item
in the list I'm iterating, a BOOL passed by value, and a BOOL passed by
reference.  Is it possible to replace my for() loop with for_each() in this
case?


Quote:


> > If so, how do you do it?

> It depends on what exactly you're asking.  Yes, for_each can be used with
> multi-argument functions, given certain conditions and caveats.

> What exactly do you want to pass to this multi-argument function?  Tell us
> that and we'll tell you whether you can do it or what your alternatives
are.

> Ken



Mon, 19 Dec 2005 23:35:59 GMT  
 Can for_each() be used with multi-argument functions?

Quote:
> I have a member function that takes three parameters: a pointer to an item
> in the list I'm iterating, a BOOL passed by value, and a BOOL passed by
> reference.  Is it possible to replace my for() loop with for_each() in this
> case?

With that many arguments, there's no one liner replacement (in the standard
library.  Boost will allow you to bind that many arguments).  However, you can
do it:

struct MyFunc {
  MyFunc(MyObj1& obj1, BOOL byval, BOOL& byref) :
    obj(obj), byval(byval), byref(byref)
    {}
  void operator()(MyObj2* obj2) {
    obj1.memberFunction(obj2,byval,byref);
  }
private:
  MyObj1& obj1;
  BOOL byval;
  BOOL& byref;

Quote:
};

// ...
vector<MyObj2*> myVect;
MyObj1 myObj;
BOOL boolVar;
// ...
for_each(myVect.begin(),myVect.end(),MyFunc(obj1,TRUE,boolVar));

Ken



Tue, 20 Dec 2005 00:38:41 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Using for_each with member functions

2. defining function whose type same as its arguments and used as another's argument

3. Using getopt to parse multi-argument options

4. Beginner [Q] Using Canned File Open/Save dialog

5. confused about multi-threaded applications using static functions

6. multi-threaded applications using static functions

7. multi-threaded applications using static functions

8. confused about multi-threaded applications using static functions

9. using functions as arguments

10. for_each, member functions, and this pointers

11. template arguments used within a template function - how?

12. Using Pointers to Handles as Function Arguments

 

 
Powered by phpBB® Forum Software