473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how second and first arg are binded?

hello,

I'm reading <the C++ programming language>, here's a question for bind2nd.
Who can help explain what on earth bind2nd is working?

....
void rotate_all( list<Shape*>& ls, int angle )
{

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

}

This code is very easy to understand, but when i tried to use bind2nd and
mem_fun() source code to see what's going on here, I don't understand how
"angle" is binded as second argument? And how the first arg is binded as
Shape* under the condition of op(x, arg2) of binder2nd?

Thanks,
Hunter
Jul 22 '05 #1
8 1491
Hunter Hou wrote:
I'm reading <the C++ programming language>, here's a question for bind2nd.
Who can help explain what on earth bind2nd is working?

...
void rotate_all( list<Shape*>& ls, int angle )
{

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

}

This code is very easy to understand, but when i tried to use bind2nd and
mem_fun() source code to see what's going on here, I don't understand how
"angle" is binded as second argument? And how the first arg is binded as
Shape* under the condition of op(x, arg2) of binder2nd?


All those elements of the standard library are templates. Please take
a look at the headers where those things are defined, it should be easy
enough to understand. Whenever in doubt, use the source.

V
Jul 22 '05 #2
Hunter Hou napsal(a):
hello,

I'm reading <the C++ programming language>, here's a question for bind2nd.
Who can help explain what on earth bind2nd is working?

...
void rotate_all( list<Shape*>& ls, int angle )
{

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

}

This code is very easy to understand, but when i tried to use bind2nd and
mem_fun() source code to see what's going on here, I don't understand how
"angle" is binded as second argument? And how the first arg is binded as
Shape* under the condition of op(x, arg2) of binder2nd?

Thanks,
Hunter


Hello,

short explaintation:

bind2nd convert function which takes two arguments into function which
takes only 1 argument and call the original with 2nd argument replaced
by constant.

definitions:

If f is an object of class binder2nd<AdaptableBinaryFunction>, then f(x)
returns F(x, c), where F is an object of class AdaptableBinaryFunction
and where c is a constant. Both F and c are passed as arguments to
binder2nd's constructor.

example:

Suppose you have function which takes two arguments - say:

void my_function(my_type& my_object, int some_number);

Then you have vector of my_type objects:

vector<my_type> my_vector(10);

and you want to apply my_function() to all bojects in the vector with
constant second argument (equal to 5). There is STL function for_each
which can help, but it has chatch: it takes only unary function so you
have to:

a) define new version of your function

void my_function2(my_type& my_object) {
my_function(my_object, 5);
}

This has the disadvantage that you can't change the value of second
parameter. To overcome this you can also:

b) define functor which uses your function

class my_functor
{
private:
int second_argument;
public:
my_functor(int init_second_argument)
: second_argument(init_second_argument) {};
operator()(my_type& first_argument)
{
my_function(first_argument, second_argument);
};
}

and use it:

my_functor(5) f;

f(my_object); // this equals to: my_function(my_object, 5);

binder2nd does actually same thing, but can save you the typing...

3) Use function adaptor binder2nd

which will turn your function into unary function "on-line"

bind2nd(my_function(), 5) (my_object);
^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
new functor created argument

Compare the final results:

1) ------------------

void my_function(my_type& my_object, int some_number);

void my_function2(my_type& my_object) {
my_function(my_object, 5);
}

int main()
{
vector<my_type> my_vector(10);

for_each(my_vector.begin(), my_vector.end(), my_function2());
}

2) ------------------

void my_function(my_type& my_object, int some_number);

class my_functor
{
private:
int second_argument;
public:
my_functor(int init_second_argument)
: second_argument(init_second_argument) {};
operator()(my_type& first_argument)
{
my_function(first_argument, second_argument);
};
}

int main()
{
vector<my_type> my_vector(10);

for_each(my_vector.begin(), my_vector.end(), my_functor(5));
}

3) ------------------

void my_function(my_type& my_object, int some_number);

int main()
{
vector<my_type> my_vector(10);

for_each(my_vector.begin(), my_vector.end(),
bind2nd(my_function(), 5));
}

---------------------

For more information see http://www.sgi.com/tech/stl/binder2nd.html

Have a nice day. Ales
Jul 22 '05 #3

Hello,

short explaintation:

bind2nd convert function which takes two arguments into function which
takes only 1 argument and call the original with 2nd argument replaced
by constant.

definitions:

If f is an object of class binder2nd<AdaptableBinaryFunction>, then f(x)
returns F(x, c), where F is an object of class AdaptableBinaryFunction
and where c is a constant. Both F and c are passed as arguments to
binder2nd's constructor.


Thanks! Ales. I understand what you said since you are talking about
function with 2 args(acturally i read source code of bind2nd() ). What i
didn't figure out is how bind2nd works with mem_fun() because member
function has one arg while bind2nd() deals with two args function.

Could you give an example like what you did above?

Regards,
Hunter
Jul 22 '05 #4
void rotate_all( list<Shape*>& ls, int angle )
{

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

}

This code is very easy to understand, but when i tried to use bind2nd and
mem_fun() source code to see what's going on here, I don't understand how
"angle" is binded as second argument? And how the first arg is binded as
Shape* under the condition of op(x, arg2) of binder2nd?

All those elements of the standard library are templates. Please take
a look at the headers where those things are defined, it should be easy
enough to understand. Whenever in doubt, use the source.

Yes. This question came out after I read the source code. bind2nd( arg1,
arg2) .arg1 is a binary functor, but rotate( int ) only has one arg.

My udnerstanding of above code is
bind2nd( mem_fun( &Shape::rotate ), angle ) ( obj ),

next is
mem_fun( &Shape::rotate ) ( obj, angle )

but what's next? How is obj handled since member function has only one
argument?

Hope i can explain my question clearly.

thanks,
Hunter
===
Jul 22 '05 #5

"Hunter Hou" <hy***@lucent.com> wrote in message
news:cf********@netnews.proxy.lucent.com...
void rotate_all( list<Shape*>& ls, int angle )
{

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

}

This code is very easy to understand, but when i tried to use bind2nd and mem_fun() source code to see what's going on here, I don't understand how "angle" is binded as second argument? And how the first arg is binded as Shape* under the condition of op(x, arg2) of binder2nd?

All those elements of the standard library are templates. Please take
a look at the headers where those things are defined, it should be easy
enough to understand. Whenever in doubt, use the source.

Yes. This question came out after I read the source code. bind2nd( arg1,
arg2) .arg1 is a binary functor, but rotate( int ) only has one arg.

My udnerstanding of above code is
bind2nd( mem_fun( &Shape::rotate ), angle ) ( obj ),

next is
mem_fun( &Shape::rotate ) ( obj, angle )

but what's next? How is obj handled since member function has only one
argument?

Hope i can explain my question clearly.

thanks,
Hunter
===


Shape::rotate has only one arg but mem_fun( &Shape::rotate ) is a functor
with two args. The first argument to mem_fun( &Shape::rotate ) is a pointer
to the shape you want to rotate, then second arg is the angle.

john

Jul 22 '05 #6
In article <cf********@netnews.proxy.lucent.com>,
Hunter Hou <hy***@lucent.com> wrote:
void rotate_all( list<Shape*>& ls, int angle )
{

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

}

This code is very easy to understand, but when i tried to use bind2nd and
mem_fun() source code to see what's going on here, I don't understand how
"angle" is binded as second argument? And how the first arg is binded as
Shape* under the condition of op(x, arg2) of binder2nd?

All those elements of the standard library are templates. Please take
a look at the headers where those things are defined, it should be easy
enough to understand. Whenever in doubt, use the source.

Yes. This question came out after I read the source code. bind2nd( arg1,
arg2) .arg1 is a binary functor, but rotate( int ) only has one arg.

My udnerstanding of above code is
bind2nd( mem_fun( &Shape::rotate ), angle ) ( obj ),

next is
mem_fun( &Shape::rotate ) ( obj, angle )

but what's next?


inside mem_fun( &Shape::rotate ) ( obj, angle ) is

obj->rotate( angle )

You can tell this because of the source of mem_fun1_t:

template <class _Ret, class _Tp, class _Arg>
class mem_fun1_t : public binary_function<_Tp*,_Arg,_Ret> {
public:
explicit mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
_Ret operator()(_Tp* __p, _Arg __x) const { return (__p->*_M_f)(__x); }
private:
_Ret (_Tp::*_M_f)(_Arg);
};
Jul 22 '05 #7
Hunter Hou napsal(a):

Thanks! Ales. I understand what you said since you are talking about
function with 2 args(acturally i read source code of bind2nd() ). What i
didn't figure out is how bind2nd works with mem_fun() because member
function has one arg while bind2nd() deals with two args function.

Could you give an example like what you did above?

Regards,
Huntervoid rotate_all( list<Shape*>& ls, int angle )


OK, let's thake it step-by-step:

mem_fun( &Shape::rotate ) will create function (it is actually functor,
but it does not matter) which takes two arguments. First argument is
instance of Shape, second is angle - or more precisely Shape::rotate
argument. The function then calls member Shape::rotate with angle as
it's argument. You can imagine it as follows:

int call_shape_member(Shape& my_shape, int angle)
{
return my_shape.rotate(angle);
};

(I suppose that Shape::rotate argument type is int as well as it's
return type).

So you have non-member function which takes Shape& and int. Next you
convert it to unary function by binding the second parameter to constant.

bind2nd( mem_fun( &Shape::rotate ), angle)

Now you have unary function which:

1) takes Shape& as argument
2) call Shape::rotate with angle as argument

You apply it to all elements of list

for_each( ls.begin(), ls.end(), bind2nd( mem_fun( &Shape::rotate ),
angle ));

And that's it. Simple. :-)

BTW: Are you sure about the list<Shape*>? Shouldn't it be list<Shape>?

Ales
Jul 22 '05 #8
AlesD napsal(a):
BTW: Are you sure about the list<Shape*>? Shouldn't it be list<Shape>?


Hmm - probably not.

A.
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: charliewest | last post by:
I have binded a dropdownlist control to a data view. I am trying to add a listitem to this already binded control, however, i cannot figure this out. My code is: // code to create data view...
0
by: Amar | last post by:
I have a simple TextBox and i have Binded with a column through design mode. In my code i have used TextBox1.DataBind() Is there any way to determine in runtime at which column of my table the...
2
by: Ville Mattila | last post by:
Hi there, I will post my question to this group too bacause the .data group seems to be rather quiet. I've been playing with VB.NET and ADO for a week now and find the different data handling...
3
by: Grizlyk | last post by:
Hello. I am thinking: why if i will comment lines marked with '#' in the following example, all will be compiled, else will not: // *********************** typedef unsigned uint; ...
4
by: billiejoex | last post by:
Hi, I'm writing a small asyncore-based server application serving a lot of clients. When I have to handle more than 1021 client simoultaneously the 'binded' socket object raises an error: ...
6
by: hydson | last post by:
Hi, I have two gridviews to which data is binded when page is not posted back. when i run for first time the data is binded to gridview, and when i run next time the data is not binded to grid...
6
by: cmrchs | last post by:
Hello, I have set the Allowpaging property to true in a standard DataGrid- control (System.Web.UI.WebControls). when run in the browser i get the first records in the first page, there are 5...
3
by: Dream | last post by:
The code create 2 windows. 2 radiobuttons are put on the second window. A control variable "v" is binded to the 2 widgets. But when I run the code, I found the control variable not binded...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.