A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/iterator/istream_iterator/ below:

class template

<iterator>

std::istream_iterator
template <class T, class charT=char, class traits=char_traits<charT>,           class Distance = ptrdiff_t>  class istream_iterator;

Istream iterator


Istream iterators are input iterators that read successive elements from an input stream (such as cin).

They are constructed from a basic_istream object, to which they become associated, so that whenever operator++ is used on the iterator, it extracts an element from the stream (using operator>>).

This kind of iterator has a special state as an end-of-stream iterator, which is acquired if an input operations fails (as returned by fail after an operation with the associated stream), and is also the resulting value of a default-constructed object.



Template parameters
T
Element type for the iterator: The type of elements extracted from the stream
charT
First template parameter of the associated basic_istream object: The type of elements the stream handles (char for istream).
traits
Second template parameter of the associated basic_istream: Character traits for the elements the stream handles.
Distance
Type to represent the difference between two iterators (generally a signed integral type, such as ptrdiff_t).
Note: The default template arguments correspond to an instantiation that uses an istream object as associated stream.

Member types member definition in istream_iterator description istream_type basic_istream<charT,traits> Type of the associated input stream iterator_category input_iterator_tag Input iterator value_type T Type of the elements pointed by the iterator char_type charT Type of the characters handled by the associated stream traits_type traits Character traits for associated stream difference_type Distance Distance type pointer const T* reference const T&
Member functions
(constructor)
Construct istream iterator (public member function)
operator*
Dereference iterator (public member function)
operator->
Dereference iterator (public member function)
operator++
Increment iterator position (public member function)

Non-member function overloads
relational operators
Relational operators for istream_iterator (function template)

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// istream_iterator example
#include <iostream>     // std::cin, std::cout
#include <iterator>     // std::istream_iterator

int main () {
  double value1, value2;
  std::cout << "Please, insert two values: ";

  std::istream_iterator<double> eos;              // end-of-stream iterator
  std::istream_iterator<double> iit (std::cin);   // stdin iterator

  if (iit!=eos) value1=*iit;

  ++iit;
  if (iit!=eos) value2=*iit;

  std::cout << value1 << "*" << value2 << "=" << (value1*value2) << '\n';

  return 0;
}

Possible output:
Please, insert two values: 2 32
2*32=64


Possible implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template <class T, class charT=char, class traits=char_traits<charT>, class Distance=ptrdiff_t>
  class istream_iterator :
    public iterator<input_iterator_tag, T, Distance, const T*, const T&>
{
  basic_istream<charT,traits>* in_stream;
  T value;

public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef basic_istream<charT,traits> istream_type;
  istream_iterator() : in_stream(0) {}
  istream_iterator(istream_type& s) : in_stream(&s) { ++*this; }
  istream_iterator(const istream_iterator<T,charT,traits,Distance>& x)
    : in_stream(x.in_stream), value(x.value) {}
  ~istream_iterator() {}

  const T& operator*() const { return value; }
  const T* operator->() const { return &value; }
  istream_iterator<T,charT,traits,Distance>& operator++() {
    if (in_stream && !(*in_stream >> value)) in_stream=0;
    return *this;
  }
  istream_iterator<T,charT,traits,Distance> operator++(int) {
    istream_iterator<T,charT,traits,Distance> tmp = *this;
    ++*this;
    return tmp;
  }
};

See also
ostream_iterator
Ostream iterator (class template)
input_iterator_tag
Input iterator category (class)
istream
Input stream (class)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4