Question regarding mem_fn or function
Hi, My imagination fails me here: How can you use boost::mem_fn or boost::function to bind a class member function as a predicate function for use in std::sort? I tried this way and that and so far, nothing. Thanks, Elisha Berns e.berns@computer.org
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)); Thanks, Elisha Berns
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Peter Dimov Sent: Thursday, June 16, 2005 1:30 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Question regarding mem_fn or function
Elisha Berns wrote:
Hi,
My imagination fails me here:
How can you use boost::mem_fn or boost::function to bind a class member function as a predicate function for use in std::sort? I tried this way and that and so far, nothing.
Please post a short example. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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 ) );
Thanks, That did it. Elisha
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Peter Dimov Sent: Thursday, June 16, 2005 6:15 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Question regarding mem_fn or function
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Elisha Berns
-
Peter Dimov