Namespaces
Variants
Views
Actions

std::istream_iterator::istream_iterator

From cppreference.com
constexpr istream_iterator();
(1)
istream_iterator( istream_type& stream );
(2)
istream_iterator( const istream_iterator& other ) = default;
(3)
1) Constructs the end-of-stream iterator. This constructor is constexpr if std::is_trivially_default_constructible_v<T> is true .
2) Initializes the iterator, stores the address of stream in a data member, and performs the first read from the input stream to initialize the cached value data member.
3) Constructs a copy of other. If std::is_trivially_copy_constructible_v<T> is true , this copy constructor is a trivial copy constructor.

[edit] Parameters

stream - stream to initialize the istream_iterator with
other - another istream_iterator of the same type

[edit] Notes

The specification formally permits std::istream_iterator to delay the first read to take place when the first dereferencing is done. This was reaffirmed by lwg245, but contested by p0738 as unimplementable. All existing implementations perform the first read immediately, in the constructor.

[edit] Examples

#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
int main()
{
    std::istringstream stream("1 2 3 4 5");
    std::copy(
        std::istream_iterator<int>(stream),
        std::istream_iterator<int>(),
        std::ostream_iterator<int>(std::cout, " ")
    );
}

Output:

1 2 3 4 5