How can I bind two argument member function?

B

BekTek

I have class that has member function that take two argument..
I'd like to use the member function to algorithm such as remove_if..

How can I bind that?

class Foo{

void f(){
int a = 10;
remove_if(list.begin(), list.end(), HERE);
}

bool someFunc(int a, int b){
return a==b;
}

list<int> list;
}

What I want is to remove all elems which is 10;
This is short example..

Do you know how to do it?
 
J

John Harrison

BekTek said:
I have class that has member function that take two argument..
I'd like to use the member function to algorithm such as remove_if..

How can I bind that?

class Foo{

void f(){
int a = 10;
remove_if(list.begin(), list.end,HERE
}

bool someFunc(int a, int b){
return a==b;
}

list<int> list;
}

What I want is to remove all elems which is 10;
This is short example..

Do you know how to do it?

Like this?

class Foo
{
void f();
bool someFunc(int a, int b)
{
return a==b;
}
list<int> list;
};

struct FooBinder
{
FooBinder(Foo* foo, bool (Foo::* func)(int, int), int a)
{
this->foo = foo;
this->func = func;
this->a = a;
}
bool operator()(int b) const
{
return (foo->*func)(a, b);
}
Foo* foo;
bool (Foo::* func)(int, int);
int a;
};

Untested, but it does compile.

john
 
J

John Harrison

Should add

void Foo::f()
{
remove_if(list.begin ( ) , list.end ( ) , FooBinder(this, &Foo::someFunc,
10));
}

john
 
J

John Harrison

BekTek said:
Your idea is so excellent..
But What I wanted was to use STL function object..
:)

struct FooBinder : public std::unary_function<int, bool>
{
...

Now it is a STL function object.

I find the STL classes and functions like bind1st and mem_fun really
confusing, so I usually roll my own. It might be possible to use some
combination of standard classes to get what you want, but all those standard
class would be doing internally is what I coded in FooBinder.

john
 
I

Ioannis Vranos

BekTek said:
I have class that has member function that take two argument..
I'd like to use the member function to algorithm such as remove_if..

How can I bind that?



What you are talking here is a member function adapter. The standard
library provides adapters of this kind for no-arguments and 1-argument.


As TC++PL says:

"The standard library need not deal with member functions taking more
than one argument because no standard library algorithm takes a function
with more than two arguments as operands."



class Foo{

void f(){
int a = 10;
remove_if(list.begin(), list.end(), HERE);
}

bool someFunc(int a, int b){
return a==b;
}

list<int> list;
}

What I want is to remove all elems which is 10;
This is short example..

Do you know how to do it?


In general your code does not make sense. If you want to compare the
members of list against a specific value -by making someFunc() a regular
function, since it does not make sense as a member function- we get:


Binder generators that can be used:

bind2nd Call binary function with y as 2nd argument.
bind1st Call binary function with x as 1st argument.



Function adapter generator:

ptr_fun



The code fixed becomes:


#include <algorithm>
#include <functional>
#include <list>

inline bool SomeFunc (const int a, const int b)
{
return a==b;
}



class Foo
{
std::list<int> someList;

public:

void f()
{
using namespace std;
int a = 10;
remove_if(someList.begin(), someList.end(),
bind2nd(ptr_fun(SomeFunc), 10));
}
};




It doesn't compile with VS though.
 
I

Ioannis Vranos

Ioannis said:
The code fixed becomes:


#include <algorithm>
#include <functional>
#include <list>

inline bool SomeFunc (const int a, const int b)
{
return a==b;
}



class Foo
{
std::list<int> someList;

public:

void f()
{
using namespace std;
int a = 10;
remove_if(someList.begin(), someList.end(),
bind2nd(ptr_fun(SomeFunc), 10));
}
};




It doesn't compile with VS though.


I forgot to mention that the standard library provides useful predicates
in <functional> including equal_to. So the above more elegantly becomes:



#include <algorithm>
#include <functional>
#include <list>

class Foo
{
std::list<int> someList;

public:

void f()
{
using namespace std;

int a = 10;

remove_if(someList.begin(), someList.end(), bind2nd(equal_to<int>(),
a));
}
};





which compiles in VS too, but with many bizarre warnings.
 
T

Tom Widmer

I have class that has member function that take two argument..
I'd like to use the member function to algorithm such as remove_if..

How can I bind that?

class Foo{

void f(){
int a = 10;
remove_if(list.begin(), list.end(), HERE);
}

bool someFunc(int a, int b){
return a==b;
}

That function could be a static member, removing the problem. But
assuming you've over-simplified, and it can't be static, read on.
list<int> list;
}

What I want is to remove all elems which is 10;
This is short example..

Do you know how to do it?

The standard is soon going to be extended with component called
std::tr1::bind (see
http://www.open-std.org/jtc1/sc22/wg21/docs/library_technical_report.html)

For the time being you can get it as part of boost (www.boost.org).
Then you can do:

remove_if(list.begin(), list.end(),
boost::bind(&Foo::someFunc, this, _1, 10));

Tom
 
D

Daniel T.

BekTek said:
I have class that has member function that take two argument..
I'd like to use the member function to algorithm such as remove_if..

How can I bind that?

class Foo{

void f(){
int a = 10;
remove_if(list.begin(), list.end(), HERE);
}

bool someFunc(int a, int b){
return a==b;
}

list<int> list;
}

What I want is to remove all elems which is 10;
This is short example..

Do you know how to do it?

You want to remove all elems which is 10...

class Foo {
list< int > myList;

void f() {
remove_if( myList.begin(), myList.end(),
bind1st( equal_to< int >(), 10 ) );
}
};

I can see, however, that you want to use 'someFunc' in the remove_if
algorithm; but where does the 'b' argument come from? How can we
possibly know what 'b' is supposed to be?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top