c++ - Help understanding boost::bind placeholder arguments -
i reading stackoverflow post regarding sorting vector of pairs second element of pair. obvious answer create predicate, 1 answer used boost caught eye.
std::sort(a.begin(), a.end(), boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2));
i've been trying figure out how boost::bind works, or @ least how use it, can't figure out purpose of placeholder arguments _1 , _2 are, , boost documentation doesn't sink in @ all.
could explain specific usage of boost::bind?
p.s. original question: how sort vector of pairs based on second element of pair?
this expression:
boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)
namely, use of <
operator defines functor between 2 other functors, both of defined bind
.
the functor expected sort needs have operator()
looks this:
bool operator()(const t& arg1, const t& arg2);
when you're creating functor using boost's <
name holders _1
, _2
correspond arg1
, arg2
of functor you're creating.
the bind
call create functor calls ::second
of arg1
, arg2
with luck, introduction of lambdas in c++0x make expressions obsolete.
Comments
Post a Comment