Topic: how to find the iterator's type?


Author: hinnant@anti-spam_metrowerks.com (Howard Hinnant)
Date: 1999/11/22
Raw View
In article <007601bf326c$76706f40$e60afea9@n86t1>, "Stephane Rondal"
<rondal@usa.net> wrote:

> //////////////////////////////////////////////////////////////
> template<class InputIterator>
>  void dump( ostream& os, InputIterator first, InputIterator last )
> {
>
>  ostream_iterator< ????? > osi( os, "\n" );
>  copy( first, last, osi );
>
> }
>
> Now my question is how do I declare ostream_iterator in dump()
> so that it can guess the InputIterator's type?
> I mean this shouldn't be hard-coded as ostream_iterator<string>.

template<class InputIterator>
 void dump( ostream& os, InputIterator first, InputIterator last )
{
 typedef typename iterator_traits<InputIterator>::value_type T;
 ostream_iterator<T> osi( os, "\n" );
 copy( first, last, osi );

}

-Howard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jbarfurth@vossnet.de (Joerg Barfurth)
Date: 1999/11/22
Raw View
Stephane Rondal <rondal@usa.net> wrote:

> I'm trying to put up the following dump() function:

> template<class InputIterator>
>  void dump( ostream& os, InputIterator first, InputIterator last )
> {
>  ostream_iterator< ????? > osi( os, "\n" );
>  copy( first, last, osi );
> }

> Now my question is how do I declare ostream_iterator in dump()
> so that it can guess the InputIterator's type?

use
    typename iterator_traits<InputIterator>::value_type
and don't forget to
    #include <iterator>

> Thanks,
> Stephen Rondal

-- J=F6rg Barfurth
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ross Smith <ross.s@ihug.co.nz>
Date: 1999/11/20
Raw View
Stephane Rondal wrote:
>
> I'm trying to put up the following dump() function:
>
> template<class InputIterator>
>  void dump( ostream& os, InputIterator first, InputIterator last )
> {
>  ostream_iterator< ????? > osi( os, "\n" );
>  copy( first, last, osi );
> }

This sort of thing is what the iterator_traits template is for:

    ostream_iterator<iterator_traits<InputIterator>::value_type>
        osi(os, "\n");

--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
  "There are many technical details that make Linux attractive to the
  sort of people to whom technical details are attractive."   -- Suck


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Stephane Rondal" <rondal@usa.net>
Date: 1999/11/19
Raw View
I'm trying to put up the following dump() function:

//////////////////////////////////////////////////////////////
template<class InputIterator>
 void dump( ostream& os, InputIterator first, InputIterator last )
{

 ostream_iterator< ????? > osi( os, "\n" );
 copy( first, last, osi );

}
//////////////////////////////////////////////////////////////

Here's sample code calling the dump() function

vector<string> v;
 v.push_back( "abc" );
 v.push_back( "def" );
dump( cout, v.begin(), v.end() );

//////////////////////////////////////////////////////////////

Now my question is how do I declare ostream_iterator in dump()
so that it can guess the InputIterator's type?
I mean this shouldn't be hard-coded as ostream_iterator<string>.

Thanks,
Stephen Rondal




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]