帮助我理解 boost::bind 的用法
请查看 Johannes Schaub 发布的示例,对向量对进行排序:
std::sort(a.begin(), a.end(),
boost::bind(&std::pair<int, int>::second, _1) <
boost::bind(&std::pair<int, int>::second, _2));
我想我确实理解 boost: :bind,但我在这方面遇到了麻烦。
问题 1:
排序算法需要谓词函数作为第三个参数。我在这里看到的是一个布尔表达式。我缺少什么?:
boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)
boost::bind 库是否重载运算符<对于这两个绑定,并且返回某种函数指针(如 lambda)?
问题2:
这让我很困惑:
boost::bind(&std::pair<int, int>::second, _1)
通常有某种函数指针作为绑定调用的第一个参数,但这里它是类成员的地址?该特定绑定的结果是什么?
感谢您的宝贵时间&帮助
Please have a look at this example posted by Johannes Schaub to sort a vector of pairs:
How do I sort a vector of pairs based on the second element of the pair?
std::sort(a.begin(), a.end(),
boost::bind(&std::pair<int, int>::second, _1) <
boost::bind(&std::pair<int, int>::second, _2));
I thought I do understand boost::bind, but I have trouble with this one.
Question 1:
the sort algorithm is expecting a predicate function as a third parameter. What I see here, is a boolean expression. What am I missing?:
boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)
Does the boost::bind library overload operator< for those two binds, and is returning some kind of function pointer (like a lambda)?
Question 2:
This gets me confused:
boost::bind(&std::pair<int, int>::second, _1)
Usually there is some kind of function pointer as first parameter of a bind call, but here it is an address of a class member? What is the result of that particular bind?
Thanks for your time & help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost::bind 重载了运算符!以及关系和逻辑运算符 ==、!=、<、<=、>、>=、&&、||,这就是为什么您“看到”布尔表达式,但您真正返回一个函数谓词。
从那里您可以看到您正在为重载的小于函数的第一个和第二个参数绑定该对的第二个成员。
至于你的第二个问题:
当您将指针传递给成员时,Boost Bind 将识别并将其视为您调用了
文档是这样描述的:
您可以找到此信息以及更多信息
boost::bind overloads the operator ! and the relational and logical operators ==, !=, <, <=, >, >=, &&, ||, so this is why you "see" a boolean expression, but you're really getting back a function predicate.
From there you can see that you're binding the second member of the pair for the 1st and 2nd arguments of the overloaded less than function.
As for your second question:
Boost bind will recognize when you have passed a pointer to a member and treat it as if you called
This is how the documentation describes this:
You can find this and more information here.
您的理论是正确的,请参阅:
http://www. boost.org/doc/libs/1_44_0/libs/bind/bind.html#operators
Your theory is correct, see:
http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html#operators