Boost.Bind allows in simple cases to define a compare function inplace:
#include <iostream>
#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/bind.hpp>
#define P(a) do { \
BOOST_FOREACH (Struct s, a) \
std::cout << boost::format("(%d %c) ") % s.i % s.c; \
std::cout << std::endl; \
} while(0)
namespace {
struct Struct { int i; char c; };
}
int main() {
using boost::bind;
Struct a[] = { 1, 'z', 2, 'a' }; P(a);
const int N = sizeof(a) / sizeof(*a);
std::sort(a, a + N, bind(&Struct::i, _1) > bind(&Struct::i, _2)); P(a);
std::sort(a, a + N, bind(&Struct::c, _1) > bind(&Struct::c, _2)); P(a);
}
Output:
(1 z) (2 a)
(2 a) (1 z)
(1 z) (2 a)