Boost logo

Boost :

From: Noel Yap (yap_noel_at_[hidden])
Date: 2002-05-20 18:35:39


--- David White <dave_at_[hidden]> wrote:
> On Mon, 2002-05-20 at 23:13, Noel Yap wrote:
> > --- David White <dave_at_[hidden]> wrote:
> > Currently, I've created a function copy_n that
> copies
> > at most 'n' elements.
>
> So do I. Unfortunately this function template has
> the same name as a
> function template in a common STL implementation
> (STLPort) which has
> slightly different functionality; this can cause
> confusion.
>
> I can see other '_n' type
> > functions. iterator_n would make such functions
> moot
> > so iterator_n would be a great addition to Boost.
> Is
> > there an effort to add this into Boost?
>
> not at this stage, I only just posted the idea;
> naturally there could be
> if enough people said they thought it was a good
> idea...

I thought it (iterator_n) is such a great idea that I
got rid of my copy_n function in favor of a newly
implemented iterator_n adapter.

I have some minor differences than the one you
described. Here's the source if you're interested:

    template < typename ForwardIterator, typename Size
= std::size_t >
    class iterator_n
    : private boost::iterator<
std::forward_iterator_tag, typename
std::iterator_traits< ForwardIterator >::value_type,
typename std::iterator_traits< ForwardIterator
>::difference_type >,
    private boost::forward_iteratable< iterator_n<
ForwardIterator >, typename std::iterator_traits<
ForwardIterator >::pointer >
    {
        private: ForwardIterator iter;
        private: Size n;

        private: void constraints(void)
        {
            boost::function_requires< typename
boost::ForwardIteratorConcept< ForwardIterator > >();
        }

        public: iterator_n()
        : n(0)
        {
        }

        public: iterator_n(ForwardIterator iter, Size
n = 0)
        : iter(iter),
        n(n)
        {
        }

        public: typename std::iterator_traits<
ForwardIterator >::value_type &operator *(void) const
        {
            return *iter;
        }

        public: iterator_n &operator ++(void)
        {
            ++iter;
            --n;
        }

        public: bool operator ==(const iterator_n
&rhs) const
        {
            return iter == rhs.iter
            || n == rhs.n;
        }
    };

As you can see, it has a default constructor so that
it can be usable in situations where there is no "end"
iterator.

There's probably a lot of room for improvement so I
welcome any comments.

Noel

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com


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