帮助理解的boost ::绑定参数占位符 [英] Help understanding boost::bind placeholder arguments

查看:102
本文介绍了帮助理解的boost ::绑定参数占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读关于由对的第二个元素排序对的矢量计算器一个岗位。最明显的答案是创建一个predicate,但使用升压一个答案引起了我的眼睛。

I was reading a StackOverFlow post regarding sorting a vector of pairs by the second element of the pair. The most obvious answer was to create a predicate, but one answer that used boost caught my eye.

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

我一直在试图找出如何提高::绑定工作,或者至少是如何使用它,但我无法弄清楚什么的占位符参数的目的_1和_2是,和升压文档在所有不沉沦

I've been trying to figure out how boost::bind works, or at least just how to use it, but I can't figure out what the purpose of the placeholder arguments _1 and _2 are, and the boost documentation doesn't sink in at all.

谁能解释提振这个具体用法::绑定?

Could anyone explain this specific usage of boost::bind?

P.S。原题:<一href=\"http://stackoverflow.com/questions/279854/how-do-i-sort-a-vector-of-pairs-based-on-the-second-element-of-the-pair\">http://stackoverflow.com/questions/279854/how-do-i-sort-a-vector-of-pairs-based-on-the-second-element-of-the-pair

推荐答案

这前pression:

This expression:

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

即利用&LT; 运营商实际上定义了另外两个函子,这两者定义为之间的函子绑定

namely, the use of the < operator actually defines a functor between two other functors, both of which defined by bind.

这是排序有望函子需要有一个操作符()看起来像这样:

The functor expected by sort needs to have an operator() which looks like this:

bool operator()(const T& arg1, const T& arg2);

当你使用创造一个仿函数

升压的&LT; 那么域名持有者 _1 _2 ARG1 ARG2 正在创建的仿函数。

when you're creating a functor using boost's < then the name holders _1 and _2 correspond to arg1 and arg2 of the functor you're creating.

绑定调用创建调用 ::第二个 ARG1 ARG2

运气好的话,在C ++ 0x中引入lambda表达式将会使前pressions这样过时了。

With any luck, the introduction of lambdas in C++0x will make expressions like this obsolete.

这篇关于帮助理解的boost ::绑定参数占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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