By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,318 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,318 IT Pros & Developers. It's quick & easy.

How can I bind two argument member function?

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"BekTek" <be****@gmail.com> wrote in message
news:VWIbd.987$u6.960@trnddc06...
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
Jul 22 '05 #2

P: n/a
Should add

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

john
Jul 22 '05 #3

P: n/a
Your idea is so excellent..
But What I wanted was to use STL function object..
:)

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de...
Should add

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

john

Jul 22 '05 #4

P: n/a

"BekTek" <be****@gmail.com> wrote in message
news:1gKbd.2199$7h.1606@trnddc07...
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
Jul 22 '05 #5

P: n/a
BekTek wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

P: n/a
Ioannis Vranos wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

P: n/a
On Fri, 15 Oct 2004 05:08:05 GMT, "BekTek" <be****@gmail.com> wrote:
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/wg...al_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
Jul 22 '05 #8

P: n/a
"BekTek" <be****@gmail.com> wrote:
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?
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.