Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2003-03-02 18:16:04


"Thorsten Ottosen" <nesotto_at_[hidden]> wrote in message
news:b3th37$gpa$1_at_main.gmane.org...
>
> "Alisdair Meredith" <alisdair.meredith_at_[hidden]> wrote in message
> news:3E6232BB.80B6D0FE_at_uk.renaultf1.com...
> > Thorsten Ottosen wrote:
> >
> > > 2. many standard algorithms take a single output iterator as an
> argument.
> > > Currently the argument for output must be a container. Should
> > > it be posible to specify an iterator here too?
> >
> > Absolutely! Not all iterators belong to containers eg ostream_iterator
>
> I see your point. Does anyone have a nice idea of how to detect when the
> template argument is an iterator? It's easy with pairs and arrays and the
> default case is containers.

The "easy" workaround is maybe to change the template argument to expect an
output iterator and not a container:

    template< typename Container1, typename Container2 >
    inline typename container_traits<Container2>::iterator
    copy( const Container1& c1, Container2& c2 )
    {
        return std::copy( begin( c1 ), end( c1 ), begin( c2 ) );
    }

becomes

    template< typename Container1, typename Output_iterator >
    inline typename container_traits<Container2>::iterator
    copy( const Container1& c1, Output_iterator c2 )
    {
        return std::copy( begin( c1 ), end( c1 ), c2 );
    }

and usage change accordingly from

copy( vector1, vector2 );

to

copy( vector1, vector2.begin() );

I see a potential benefit of the former because we can assert that the size
of the container is big enough
to hold the input. I guess debug iterators would catch the same, if you have
them.

Still, we need to detect iterators anyway if this should work:

    istream_iterator< string > is( in_file ), eof;
    typedef vector< string > vec_t;
    vec_t text;
    copy( is, back_inserter( text ) ); // same as std::copy( is, eof,
back_inserter( text ) );

We could keep the former syntax and allow inserters like this:

template< typename C >
std::back_insert_iterator<C>&
make_back_inserter( C& c )
{
    static std::back_insert_iterator<C> bii( c );
    bii = back_inserter( c );
    return bii;
}
...
copy( is, make_back_inserter( text ) );

Let me now what you think.

regards

Thorsten


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