简体   繁体   中英

bind1st and bind2nd

i had a view at the below references:

bind1st

bind2nd

what i did not understand is the difference between the two. Can anyone help me to get to know the difference.an example would be more helpful.

bind1st binds the first argument (eg you have foo(int a, int b) , then bind1st(foo, 1)(bar) will be equivalent to foo(1, bar) ), bind2nd the second one. Don't use them, though, they're nigh useless — use generalised boost::bind instead (or std::bind in C++0x).

Assume you have a function object f(x,y) and an algorithm that needs a functoid with just one variable. Then there's two possibilities:

  • Set some fixed value for y and let the algorithm work on x
  • Set some fixed value for x and let the algorithm work on y

That's the difference.

bind1st binds the first parameter of a function while bind2nd binds the second parameter. if do operation like plus() functor it will make no difference as addition of two numbers remains same in both the cases, but if u do operation like minus(), then it make difference depending upon u use bind1st or bind2nd, example 5-4 and 4-5 will generate different results, now u got the difference between bind1st first parameter binding and bind2nd second parameter binding.

That's obvious. The bind1st binds a value to the first operand of a functor (assuming you know what a functor in C++ is), bind2nd to the second. But for commutative operators as + (or std::plus ) it actually makes no difference (if you didn't overload + with non-commutative behaviour in that example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM