|
Boost : |
From: François Duranleau (duranlef_at_[hidden])
Date: 2007-08-09 11:11:08
On Thu, 9 Aug 2007, Joseph Gauterin wrote:
> Would anyone be interested in having a couple of extra specialized
> iterator adaptors in Boost.Iterator?
>
> I'm thinking of 'key_iterator' and 'value_iterator' adaptors (along
> with convenience 'make_key_iterator' and 'make_value_iterator'
> functions) that allow any associative container (that follows standard
> library idioms) to be accessed as either a range of keys or as a range
> of values.
>
> They'd be used like this:
>
> std::map<std::string,int> m;
> std::for_each(make_value_iterator(m.begin()),make_value_iterator(m.end()),func);
>
> Where func() takes an int parameter rather than a std::pair<const
> std::string,int>.
>
> Using these adaptors is considerably easier than using boost.lambda
> for this purpose and they can be implemented to incur no (or
> negligable) runtime overhead when compared to iterating normally
> through an associative container.
>
> I find these useful - does anyone else?
Yes, I agree they are. But how about a generalization to tuples? Let's
call it a get_iterator (any ideas for a better name?), i.e.
template < typename BaseIterator , int NthElement >
class get_iterator ... ;
and define make_get_iterator accordingly and then, following your example:
std::map< std::string , int > m ;
std::for_each( make_get_iterator< 1 >( m.begin() ) ,
make_get_iterator< 1 >( m.end() ) ,
func ) ;
That would require to either specialize boost::get for std::pair (easier;
wasn't a similar specialization done with tr1::get?) or specialize that
iterator for std::pairs (more code). And then, for convenience and/or
readability, it could be possible to wrap make_get_iterator as
make_key_iterator and make_value_iterator.
I already have an implementation of this get_iterator (with all
specializations for std::pair and boost::compressed_pair), if anyone is
interested.
-- François Duranleau LIGUM, Université de Montréal
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk