绑定第一和绑定第二
i had a view at the below references:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
bind1st
绑定第一个参数(例如,您有foo(int a, int b)
,然后bind1st(foo, 1)(bar)
将相当于foo(1, bar)
),bind2nd
是第二个。不过,不要使用它们,它们几乎没有用——使用通用的boost ::bind
(或 C++0x 中的std::bind
)。bind1st
binds the first argument (e.g. you havefoo(int a, int b)
, thenbind1st(foo, 1)(bar)
will be equivalent tofoo(1, bar)
),bind2nd
the second one. Don't use them, though, they're nigh useless — use generalisedboost::bind
instead (orstd::bind
in C++0x).假设您有一个函数对象
f(x,y)
和一个需要仅包含一个变量的 functoid 的算法。那么有两种可能性:y
设置一些固定值,让算法在x
上工作x
设置一些固定值,让算法工作ony
这就是区别。
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:y
and let the algorithm work onx
x
and let the algorithm work ony
That's the difference.
这是显而易见的。
bind1st
将值绑定到函子的第一个操作数(假设您知道 C++ 中的函子是什么),bind2nd
绑定到第二个操作数。但对于+
(或std::plus
)这样的交换运算符来说,它实际上没有什么区别(如果你没有用非重载+
)该示例中的交换行为)。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+
(orstd::plus
) it actually makes no difference (if you didn't overload+
with non-commutative behaviour in that example).bind1st 绑定函数的第一个参数,bind2nd 绑定第二个参数。如果执行像 plus() 函子这样的操作,则不会有任何区别,因为在两种情况下两个数字的加法保持相同,但如果您执行像 minus() 这样的操作,那么它会根据您使用 bind1st 或 bind2nd 而有所不同,示例 5- 4和4-5会产生不同的结果,现在你得到了bind1st第一个参数绑定和bind2nd第二个参数绑定之间的区别。
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.