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

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

问题描述

我正在阅读一个StackOverFlow帖子,关于通过对的第二个元素来排序对的向量。最明显的答案是创建一个谓词,但是一个使用boost的答案引起了我的注意。

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));

我一直在试图找出boost :: bind的工作原理,使用它,但是我不知道占位符参数_1和_2的目的是什么,并且boost文档根本不下沉。

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.

任何人解释这个具体使用boost :: bind?

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

原始问题: http://stackoverflow.com/questions/279854/how-do-i-sort-a-vector-of-pairs-based-on-the-second-element-of-the-pair

推荐答案

此表达式:

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

即,使用< 运算符实际上定义了两个其他函子之间的函子,它们都由 bind

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

sort需要的函数有一个 operator()

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

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



< $ c>,则名称持有者 _1 _2 对应于 arg1 arg2

bind 调用创建一个调用 :: second arg1 arg2

The bind call create a functor that calls ::second of arg1 and arg2

,在C ++ 0x中引入lambdas会使这样的表达式过时。

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

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

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