< cpp‎ | iterator
Revision as of 15:52, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <iterator>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class T,

          class CharT = char,
          class Traits = std::char_traits<CharT>,
          class Distance = std::ptrdiff_t >
class istream_iterator: public std::iterator<std::input_iterator_tag,

                                             T, Distance, const T*, const T&>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::istream_iterator is a single-pass input iterator that reads successive objects of type T from the std::basic_istream object for which it was constructed, by calling the appropriate operator>>. The actual read operation is performed when the iterator is incremented, not when it is dereferenced. The first object may be read when the iterator is constructed or when the first dereferencing is done. Otherwise, dereferencing only returns a copy of the most recently read object.

The default-constructed std::istream_iterator is known as the end-of-stream iterator. When a valid std::istream_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator. Dereferencing or incrementing it further invokes undefined behavior.

A typical implementation of std::istream_iterator holds two data members: a pointer to the associated std::basic_istream object and the most recently read value of type T.

When reading characters, std::istreambuf_iterator is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.


Member types

Member type Definition
char_type CharT
traits_type Traits
istream_type std::basic_istream<CharT, Traits>

Member functions

constructs a new istream_iterator
(public member function)
(implicitly declared)
destructs an istream_iterator, including the cached value
(public member function)
obtains a copy of the current element
accesses a member of the current element
(public member function)
advances the istream_iterator
(public member function)

Non-member functions

compares two istream_iterators
(function template)

Inherited from std::iterator

Member types

Member type Definition
value_type T
difference_type Distance
pointer const T*
reference const T&
iterator_category std::input_iterator_tag


#include <iostream>
#include <sstream>
#include <iterator>
#include <numeric>
int main()
    std::istringstream str("0.1 0.2 0.3 0.4");
                     std::ostream_iterator<double>(std::cout, " "));


0.1 0.3 0.6 1

See also

Template:cpp/iterator/dcl list ostream iteratorTemplate:cpp/iterator/dcl list istreambuf iterator