Boost logo

Boost :

From: Dan Gohman (gohmandj_at_[hidden])
Date: 2001-12-09 16:20:23


On Wed, Dec 05, 2001 at 12:52:01PM -0500, Jeremy Siek wrote:
>
> Sounds like a good idea to me!

Ok, here's an initial implementation of a select_iterator
which uses projection_iterator to iterate over a sequence
of tuples, selecting elements at a specified index.

It works for me on GCC 2.95.4 and 3.0.2 . I used
boost::remove_reference to handle tuples with references,
and std::iterator_traits, and both apparently are
portability concerns.

Dan

template<int N, typename TupleIterator>
class select_iterator_generator {

    typedef typename std::iterator_traits<TupleIterator>::value_type select_value_type;
    typedef typename boost::remove_reference<typename boost::tuples::element<N, select_value_type>::type>::type select_result_type;

    struct select : public std::unary_function<select_argument_type, select_result_type>
    {
        select_result_type& operator()(select_argument_type& t) const
        { return boost::tuples::get<N>(t); }
    };

public:
    typedef typename boost::projection_iterator_generator<select, TupleIterator>::type type;
};

template<int N, typename TupleIterator>
typename select_iterator_generator<N, TupleIterator>::type
make_select_iterator(TupleIterator base)
{
    typedef typename select_iterator_generator<N, TupleIterator>::type result_t;
    return result_t(base);
}

// example usage:

  vector<boost::tuple<int, string> > x;
  x.push_back(boost::make_tuple(11, string("eleven")));
  x.push_back(boost::make_tuple(12, string("twelve")));
  x.push_back(boost::make_tuple(10, string("ten")));

  // sorts the ints, leaves the strings alone.
  sort(make_select_iterator<0>(x.begin()),
       make_select_iterator<0>(x.end()));

-- 
Dan Gohman
gohmandj_at_[hidden]

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