Boost logo

Boost :

From: Stathi Triadis (stathie_at_[hidden])
Date: 2008-04-20 13:43:49


Hi,

I understand that the < operator is defined for tuples, but I'd like some
kind of functor that can sort tuples in a different order; for example, the
second, third, then first element of the tuple. Does boost already have
something that does this?

I was thinking something like this:

template<uint First, uint Second, uint Third>
struct SortTuple
{
    template<typename Tuple>
    bool operator()(const Tuple& tup1, const Tuple& tup2) const
    {
        if (tuples::get<First>(tup1) != tuples::get<First>(tup2))
            return tuples::get<First>(tup1) < tuples::get<First>(tup2);
        if (tuples::get<Second>(tup1) != tuples::get<Second>(tup2))
            return tuples::get<Second>(tup1) < tuples::get<Second>(tup2);
        if (tuples::get<Third>(tup1) != tuples::get<Third>(tup2))
            return tuples::get<Third>(tup1) < tuples::get<Third>(tup2);
    }
};

typedef boost::tuple<int, int, int> MyTuple;
vector<MyTuple> v;
sort(v.begin(), v.end(), SortTuple<1, 2, 0>());

This was quite easy to implement, but what about an arbitrary number of
tuple elements? Does this or something else with the equivalent
functionality exist in boost?

Cheers,
Stathi


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk