帮助我理解 boost::bind 的用法

发布于 2024-10-02 03:30:12 字数 946 浏览 3 评论 0原文

请查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-10-09 03:30:12

boost::bind 重载了运算符!以及关系和逻辑运算符 ==、!=、<、<=、>、>=、&&、||,这就是为什么您“看到”布尔表达式,但您真正返回一个函数谓词。

从那里您可以看到您正在为重载的小于函数的第一个和第二个参数绑定该对的第二个成员。

至于你的第二个问题:
当您将指针传递给成员时,Boost Bind 将识别并将其视为您调用了

bind<R>(mem_fun(&std::pair<int,int>::second), args);

文档是这样描述的:

使用带有指向成员的指针的绑定

指向成员函数的指针和
指向数据成员的指针不是
函数对象,因为它们不
支持运算符()。为了方便起见,
bind 接受成员指针作为其
第一个参数,行为如下
如果 boost::mem_fn 已被用于
将成员指针转换为
函数对象。换句话说,
表达方式

绑定(&X::f, args)

相当于

绑定(mem_fn(&X::f), args)

其中 R 是 X::f 的返回类型
(对于成员函数)或类型
成员(对于数据成员。)

您可以找到此信息以及更多信息

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

bind<R>(mem_fun(&std::pair<int,int>::second), args);

This is how the documentation describes this:

Using bind with pointers to members

Pointers to member functions and
pointers to data members are not
function objects, because they do not
support operator(). For convenience,
bind accepts member pointers as its
first argument, and the behavior is as
if boost::mem_fn has been used to
convert the member pointer into a
function object. In other words, the
expression

bind(&X::f, args)

is equivalent to

bind(mem_fn(&X::f), args)

where R is the return type of X::f
(for member functions) or the type of
the member (for data members.)

You can find this and more information here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文