|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2005-10-13 10:42:14
Simon Buchan <simon_at_[hidden]> writes:
> David Abrahams wrote:
>> Lewis Hyatt <lhyatt_at_[hidden]> writes:
>>
>>
>>>The idea is to define a template class,
>>>
>>>template<typename Iterator> class first_iterator_t {
>>> Iterator pair_iter;
>>>public:
>>> first_iterator_t(Iterator const&);
>>> /*...*/
>>>};
>>>
>>>which overloads operator*() and operator->() to access the element named
>>>"first" of the object returned by Iterator::operator*(). (With something
>>>analagous for second_iterator_t.) A function named first_iterator()
>>>provides a convenient creation function a-la std::make_pair().
>>
>>
>> Sounds like a job for transform_iterator.
>>
>> http://www.boost.org/libs/iterator/doc/transform_iterator.html
>>
> transform_iterator<(&_1)->first, Iterator>?
Not quite; an expression like that never matches a type template
parameter.
struct get_first
{
template <class T>
typename T::first_type& operator()(T& x) const
{
return x.first;
}
template <class T>
typename T::first_type const& operator()(T const& x) const
{
return x.first;
}
};
// similar get_second
transform_iterator<get_first, Iterator>
> Interesting, it could save me a couple of lines here and there... (every
> little bit helps)
Considering that most user-defined "iterators" are actually wrong for
reasons described in
http://www.boost.org/libs/iterator/doc/facade-and-adaptor.html#motivation,
it could save you more than that. :)
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk