Boost logo

Boost :

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

"Thorsten Ottosen" <nesotto_at_[hidden]> wrote in message
> "Alisdair Meredith" <alisdair.meredith_at_[hidden]> wrote in message
> > 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 ) );


    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 );


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

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 >
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.



Boost list run by bdawes at, gregod at, cpdaniel at, john at