简体   繁体   English

bind1st 和 bind2nd

[英]bind1st and bind2nd

i had a view at the below references:我对以下参考有看法:

bind1st绑定1st

bind2nd绑定2nd

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. bind1st绑定第一个参数(例如,您有foo(int a, int b) ,然后bind1st(foo, 1)(bar)将等同于foo(1, bar) ), bind2nd第二个。 Don't use them, though, they're nigh useless — use generalised boost::bind instead (or std::bind in C++0x).但是,不要使用它们,它们几乎没用——使用广义boost::bind代替(或 C++0x 中的std::bind )。

Assume you have a function object f(x,y) and an algorithm that needs a functoid with just one variable.假设您有一个 function object f(x,y)和一个只需要一个变量的 functoid 的算法。 Then there's two possibilities:那么有两种可能:

  • Set some fixed value for y and let the algorithm work on xy设置一些固定值并让算法在x上工作
  • Set some fixed value for x and let the algorithm work on yx设置一些固定值并让算法在y上工作

That's the difference.这就是区别。

bind1st binds the first parameter of a function while bind2nd binds the second parameter. bind1st 绑定 function 的第一个参数,而 bind2nd 绑定第二个参数。 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.如果像 plus() 仿函数一样进行操作,则不会有任何区别,因为在两种情况下,两个数字的加法都保持不变,但是如果您进行像 minus() 这样的操作,那么它会有所不同,具体取决于您使用 bind1st 或 bind2nd,例如 5- 4 和 4-5 会产生不同的结果,现在你得到了 bind1st 第一个参数绑定和 bind2nd 第二个参数绑定之间的区别。

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. bind1st将值绑定到函子的第一个操作数(假设您知道 C++ 中的函子是什么), bind2nd到第二个。 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).但是对于作为+ (或std::plus )的交换运算符,它实际上没有区别(如果你没有在该示例中使用非交换行为重载+ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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