Boost logo

Boost Users :

Subject: Re: [Boost-users] Iterating over the seconds of a list of pairs
From: Hicham Mouline (hicham_at_[hidden])
Date: 2009-08-17 07:01:37


> > -----Original Message-----
> > From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> > bounces_at_[hidden]] On Behalf Of Björn Karlsson
> > Sent: 08 August 2009 18:12
> > To: boost-users_at_[hidden]
> > Subject: Re: [Boost-users] Iterating over the seconds of a list of pairs
> >
> > You're right -- transform_iterator is the right tool for the job. Try
> > something like this.
> >
> > 1) Write a function object that extracts the second element of the pair.
> > 2) Use the transform iterator adaptor to provide you with the type of
> > the iterators for your class.
> > 3) Use the helper function make_transform_iterator in your begin() and
> > end() methods to create the iterators.
> >
> > Here's an example that does the above:
> >
> > #include <iostream>
> > #include <list>
> > #include <utility>
> >
> > #include "boost/iterator/transform_iterator.hpp"
> >
> > // Extract the second element of a pair
> > template <typename T> class pair_second_t {
> > public:
> > typedef T result_type;
> >
> > template <typename U> T operator()(const std::pair<U,T>& element)
> > const
> > {
> > return element.second;
> > }
> > };
> >
> > class some_class
> > {
> > public:
> > typedef std::list<std::pair<double,float> > container_type;
> > typedef boost::transform_iterator<pair_second_t<float>,
> > container_type::iterator> iterator_type;
> > Bjorn Karlsson

Hello bjorn,

I have a similar problem. I have a vector< pair<LeftT, RightT> >.
I'm very happy to use vector< pair<LeftT, RightT> >::iterator

The only extra is I'd like to name the functions GetLeft() and GetRight()
and make them callable as:

typedef vector< pair<LeftT, RightT> >::iterator stl_iterator;
stl_iterator i = ...;
i->GetLeft() and i->GetRight() // I can't do this obviously

so:
typedef boost::transform_iterator<functor, stl_iterator> my_iterator_type;

Should functor provide GetLeft() and GetRight()?
or should my_iterator_type derive from the result of
transform_iterator<functor, stl_iterator> ?

regards,


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net