c++ - Help me understand this usage of boost::bind -


please have @ example posted johannes schaub sort vector of pairs:

how sort vector of pairs based on second element of 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 understand boost::bind, have trouble one.

question 1:

the sort algorithm expecting predicate function third parameter. see here, boolean expression. missing?:

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

does boost::bind library overload operator< 2 binds, , returning kind of function pointer (like lambda)?

question 2:
gets me confused:

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

usually there kind of function pointer first parameter of bind call, here address of class member? result of particular bind?

thanks time & help

boost::bind overloads operator ! , relational , logical operators ==, !=, <, <=, >, >=, &&, ||, why "see" boolean expression, you're getting function predicate.

from there can see you're binding second member of pair 1st , 2nd arguments of overloaded less function.

as second question: boost bind recognize when have passed pointer member , treat if called

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

this how documentation describes this:

using bind pointers members

pointers member functions , pointers data members not function objects, because not support operator(). convenience, bind accepts member pointers first argument, , behavior if boost::mem_fn has been used convert member pointer function object. in other words, expression

bind(&x::f, args)

is equivalent to

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

where r return type of x::f (for member functions) or type of member (for data members.)

you can find , more information here.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -