帮助我理解这种使用boost :: bind [英] Help me understand this usage of boost::bind

查看:224
本文介绍了帮助我理解这种使用boost :: bind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看Johannes Schaub发布的此示例,以对一对向量进行排序:



如何根据第二个元素是什么?

  std :: sort(a.begin(),a.end ),
boost :: bind(& std :: pair< int,int> :: second,_1)<
boost :: bind(& std :: pair& :: second,_2));

我以为我明白boost :: bind,但是我遇到了这个问题。



问题1:



排序算法希望将谓词函数作为第三个参数。我在这里看到的是一个布尔表达式。我缺少什么?:

  boost :: bind(& std :: pair< int,int& _1) boost :: bind(& std :: pair< int,int> :: second,_2)

boost :: bind library overload operator <

问题2:
$ b $ b这让我迷惑:

  boost :: bind(& std :: pair< int,int& _1)

通常有一些函数指针作为绑定调用的第一个参数,但这里是类成员的地址?该特定绑定的结果是什么?



感谢您的时间& help

解决方案

boost :: bind重载运算符!和关系和逻辑运算符==,!=,<,< =,>,> =,&&& ;||,所以这就是为什么你看到一个布尔表达式,返回一个函数谓词。



从这里你可以看到,你绑定的第二个成员的第一和第二个参数的重载小于函数。 / p>

对于你的第二个问题:
当你传递一个指向成员的指针时,Boost bind就会识别,就像你调用

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

这是文档如何描述的:



< blockquote>

使用绑定指向成员的指针



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



bind(& X :: f,args)



等价于



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



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


您可以在此处


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

解决方案

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.

这篇关于帮助我理解这种使用boost :: bind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆