是的,你可以使用boost::bind:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <boost/bind.hpp>
struct S {
bool ascending;
bool Compare(int lhs, int rhs) {
return ascending ? (lhs < rhs) : (rhs < lhs);
}
};
int main () {
int i[] = { 1, 3, 5, 7, 8, 6, 4, 2 };
S s;
s.ascending = true;
std::sort(i, i+8, boost::bind(&S::Compare, &s, _1, _2));
std::copy(i, i+8, std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
s.ascending = false;
std::sort(i, i+8, boost::bind(&S::Compare, &s, _1, _2));
std::copy(i, i+8, std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}