   std::sort with boost::lambda
  I am trying to write an extension to a Ruby program and am trying to perform a sort using a custom function.int distance(VALUE a, VALUE b, VALUE c) {double a_x = NUM2DBL(rb_iv_get(a, "@x"));double a_y = NUM2DBL(rb_iv_get(a, "@y"));double b_x = NUM2DBL(rb_iv_get(b, "@x"));double b_y = NUM2DBL(rb_iv_get(b, "@y"));double c_x = NUM2DBL(rb_iv_get(c, "@x"));double c_y = NUM2DBL(rb_iv_get(c, "@y")); double b_dist = sqrt(pow(b_x-a_x, 2) + pow(b_y-a_y, 2));double c_dist = sqrt(pow(c_x-a_x, 2) + pow(c_y-a_y, 2));    return (b_dist < c_dist);} void find_nearest_neighbors(VALUE point, VALUE neighbors) {std::vector<VALUE> sorted_neighbors(RARRAY(neighbors)->len); std::copy(sorted_neighbors.begin(), sorted_neighbors.end(), RARRAY(neighbors)->ptr);std::sort(sorted_neighbors.begin(), sorted_neighbors.end(), boost::lambda::bind(distance)(point, boost::lambda::_1, boost::lambda::_2));}The basic idea is to be able to call 'find_nearest_neighbor' passing in a point and an array.  The weird VALUE, NUM2DBL, RARRAY, rb_iv_get all come from going back and forth from C to Ruby.Not having used boost before, I am a bit confused as to how to do the last line.  Basically, I want to sort by vector by the distance function, which should compare the distance of two inputs (N.B. I know that distance returns an int and not a boolean, but I am using a library called RubyInline which cannot account for booleans, so I am trying to get C++ to do the magic casting for me).I have seen plenty of examples of how to get bind to work with member functions, and a couple of getting it to work with _1 and _2, but never a non-member function within a std::sort.  Can someone shed some light on this for me?ThanksCorey   
  It's probably easier to use boost::bind:#include <algorithm>#include <iostream>#include <iterator>#include <boost/bind.hpp>// returns true if lhs is closer to 'from' than rhs in absolute valueint predicate(int from, int lhs, int rhs){    return abs(lhs - from) < abs(rhs - from);}int main(){    int numbers[] = { 7, 32, -234, 3432, 12, 0 };    std::sort(   numbers,    numbers + sizeof numbers / sizeof *numbers,   boost::bind(&predicate, 47, _1, _2)    );    std::copy(   numbers,   numbers + sizeof numbers / sizeof *numbers,   std::ostream_iterator<int>(std::cout, "\n")    );    return 0;}EDIT: in addition, it looks to me as if you might have your arguments to copy() the wrong way around. Currently you're copying from sorted_neighbors in to RARRAY(neighbors)->ptr.I guess you really want to initialize the vector like this:const VALUE *base = RARRAY(neighbors)->ptr;std::vector<VALUE> sorted_neighbors(base, base + RARRAY(neighbors)->len);// now sort ...;  Rates++ for the help!  The errors were getting super strange on top of the usual with boost because RubyInline seems to take the functions and put an extra 'self' argument in, so bind was expecting 4 arguments with distance, and I was only giving in three.  But it is alllll gravy now.Thanks!   ;Quote:Original post by the_eddIt's probably easier to use boost::bind:Maybe, or it can also be done with lambda...std::sort(sorted_neighbors.begin(), sorted_neighbors.end(),boost::lambda::bind(distance, point, boost::lambda::_1, boost::lambda::_2));You were trying to bind a functor object, then call it straight away, you want to create a functor object that gets called by the sort object, without calling it manually yourself.   