Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-06-16 20:15:01


Elisha Berns wrote:
> Here is a short example, that of course doesn't do anything important:
>
> class X
> {
> public:
> X() {}
> ~X() {}
>
> std::vector< int > vecInteger;
>
> bool Sort(int const& lhs, int const& rhs)
> {
> return lhs < rhs;
> }
> };
>
> // then either outside the body of the class:
>
> X x;
>
> // or inside some other class method you want to sort the vector...
>
> // and here's the part that I don't see...
>
> std::sort(x.vecInteger.begin(); x.vecInteger.end(),
> boost::mem_fn(X::Sort));

mem_fn(&X::Sort) essentially creates the equivalent of the following
function:

bool mem_fn_sort( X * this_, int const & lhs, int const & rhs )
{
    return this_->Sort( lhs, rhs );
}

Since X::Sort is a member function, it needs to operate on an object of type
X, which explains the additional argument.

std::sort needs a two-argument predicate, so assuming that you need to
invoke Sort on your original x, you should write

std::sort( x.vecInteger.begin(), x.vecInteger.end(),
    boost::bind( &X::Sort, &x, _1, _2 )
);


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net