473,761 Members | 7,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I bind two argument member function?

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
8 3403

"BekTek" <be****@gmail.c om> 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
Should add

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

john
Jul 22 '05 #3
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::someFu nc, 10));
}

john

Jul 22 '05 #4

"BekTek" <be****@gmail.c om> wrote in message
news:1gKbd.2199 $7h.1606@trnddc 07...
Your idea is so excellent..
But What I wanted was to use STL function object..
:)


struct FooBinder : public std::unary_func tion<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
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(someL ist.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
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(someL ist.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(someL ist.begin(), someList.end(), bind2nd(equal_t o<int>(),
a));
}
};

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

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7
On Fri, 15 Oct 2004 05:08:05 GMT, "BekTek" <be****@gmail.c om> 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(&Fo o::someFunc, this, _1, 10));

Tom
Jul 22 '05 #8
"BekTek" <be****@gmail.c om> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2942
by: P G | last post by:
I hope this is on topic here. I have a problem compiling a simple example of the use of boost::bind. Please take a look at the program below. /**************************************************************************/ #include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp>
1
7537
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; ...
2
3431
by: Tony Johansson | last post by:
Hello!! Can somebody explain what does argument contravariance mean. I know what covariance mean because this is involved in some way when we have argument contravariance . Use some easy code exampe to describe this. //Tony
2
2268
by: IndyStef | last post by:
I am trying to use boost's bind on a member function, on the VC8 compiler. After using several different attempts, I could not get it to work. Does anybody know what is wrong with the code below? The function that doesn't compile is foo::DoTheStuff. All three variations of the for-each loop won't build.
2
4622
by: Gert Van den Eynde | last post by:
Hi all, I have a template class template<typename TC> class A{ public: ... protected: TC ObjTC; }
4
7376
by: Damien | last post by:
Hi all, I've run into something confusing on MS VC6. Yeah I know it's old but that's what the client wants, so... I'm trying to pass a pointer to a member function as a template argument, and the compiler gives me an invalid template argument on the member function address if the member function returns a type. A member function with a void return type is fine. The example below demonstrates the problem:
7
2479
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it has nothing to do with making multiple arguments in a declaration like:
8
2301
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
3
3343
by: Giovanni Gherdovich | last post by:
Hello, in the following code I have a pointer (to function), say p, of type double (*)(double, double, void*) and I try to fix the second argument of the function *p to a given value (using boost::bind), but the compiler complains, because of a type mismatch in an assignment which I think should be legal:
0
9336
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,...
1
9902
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,...
0
9765
tracyyun
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...
0
8770
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
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...
0
6603
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();...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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
3
2738
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.