istream_iterator


An iterator that reads items in a typesafe manner from a standard input stream.

Library

Standards<ToolKit>

Declaration


#include <iterator>

template< class T, class Distance >
class istream_iterator : public input_iterator< T, Distance >

Interface

Constructor
istream_iterator()
Constructs an iterator to serve as a past-the-end value.
Constructor
istream_iterator( istream& stream )
Constructs an iterator associated with the input stream stream and caches the first element.
*
const T& operator*() const
Returns a reference to the iterator's currently cached input item.
++
istream_iterator< T, Distance >& operator++()
Reads and caches the next item in iterator's input stream. Returns a reference to the iterator.
++
istream_iterator< T, Distance > operator++( int )
Reads and caches the next item in iterator's input stream. Returns a copy of iterator's previous value.
==
bool operator==( const istream_iterator< T, Distance >& iter ) const
Returns true if the iterator has the same stream and state as iter .
!=
bool operator!=( const istream_iterator< T, Distance >& iter ) const
Returns true if the iterator does not have the same stream or state as iter .
Example <ospace/osstd/examples/istmit1.cpp>
#include <iostream>
#include <iterator>

void
main()
  {
  char buffer[ 100 ];
  int i = 0;
  cin.unsetf( ios::skipws ); // Disable white-space skipping.
  cout << "Please enter a string: ";

  istream_iterator< char, ptrdiff_t > s( cin );

  while ( *s != `\n' )
    buffer[ i++ ] = *s++;
  buffer[ i ] = `\0'; // Null terminate buffer.
  cout << "read " << buffer << "\n";
  }

Please enter a string: truth
read truth

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.