Files
2025-10-25 03:02:53 +03:00

202 lines
7.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[istream.iterator]
# 24 Iterators library [[iterators]](./#iterators)
## 24.6 Stream iterators [[stream.iterators]](stream.iterators#istream.iterator)
### 24.6.2 Class template istream_iterator [istream.iterator]
#### [24.6.2.1](#general) General [[istream.iterator.general]](istream.iterator.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6505)
The class template istream_iterator is an input iterator ([[input.iterators]](input.iterators "24.3.5.3Input iterators")) that reads successive elements
from the input stream for which it was constructed[.](#general-1.sentence-1)
namespace std {template<class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>class istream_iterator {public:using iterator_category = input_iterator_tag; using value_type = T; using difference_type = Distance; using pointer = const T*; using reference = const T&; using char_type = charT; using traits_type = traits; using istream_type = basic_istream<charT,traits>; constexpr istream_iterator(); constexpr istream_iterator(default_sentinel_t);
istream_iterator(istream_type& s); constexpr istream_iterator(const istream_iterator& x) noexcept(*see below*); ~istream_iterator() = default;
istream_iterator& operator=(const istream_iterator&) = default; const T& operator*() const; const T* operator->() const;
istream_iterator& operator++();
istream_iterator operator++(int); friend bool operator==(const istream_iterator& i, default_sentinel_t); private: basic_istream<charT,traits>* in_stream; // *exposition only* T value; // *exposition only*};}
[2](#general-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6547)
The type T shall meet the *Cpp17DefaultConstructible*,*Cpp17CopyConstructible*, and *Cpp17CopyAssignable* requirements[.](#general-2.sentence-1)
#### [24.6.2.2](#cons) Constructors and destructor [[istream.iterator.cons]](istream.iterator.cons)
[🔗](#lib:istream_iterator,constructor)
`constexpr istream_iterator();
constexpr istream_iterator(default_sentinel_t);
`
[1](#cons-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6560)
*Effects*: Constructs the end-of-stream iterator, value-initializing value[.](#cons-1.sentence-1)
[2](#cons-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6564)
*Postconditions*: in_stream == nullptr is true[.](#cons-2.sentence-1)
[3](#cons-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6568)
*Remarks*: If the initializer T() in the declaration auto x = T(); is a constant initializer ([[expr.const]](expr.const "7.7Constant expressions")),
then these constructors are constexpr constructors[.](#cons-3.sentence-1)
[🔗](#lib:istream_iterator,constructor_)
`istream_iterator(istream_type& s);
`
[4](#cons-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6582)
*Effects*: Initializes in_stream with addressof(s),
value-initializes value,
and then calls operator++()[.](#cons-4.sentence-1)
[🔗](#lib:istream_iterator,constructor__)
`constexpr istream_iterator(const istream_iterator& x) noexcept(see below);
`
[5](#cons-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6596)
*Effects*: Initializes in_stream with x.in_stream and
initializes value with x.value[.](#cons-5.sentence-1)
[6](#cons-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6601)
*Remarks*: An invocation of this constructor may be used in a core constant expression
if and only if the initialization of value from x.value is a constant subexpression ([[defns.const.subexpr]](defns.const.subexpr "3.15constant subexpression"))[.](#cons-6.sentence-1)
The exception specification is equivalent tois_nothrow_copy_constructible_v<T>[.](#cons-6.sentence-2)
[🔗](#lib:istream_iterator,destructor)
`~istream_iterator() = default;
`
[7](#cons-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6616)
*Remarks*: If is_trivially_destructible_v<T> is true,
then this destructor is trivial[.](#cons-7.sentence-1)
#### [24.6.2.3](#ops) Operations [[istream.iterator.ops]](istream.iterator.ops)
[🔗](#lib:operator*,istream_iterator)
`const T& operator*() const;
`
[1](#ops-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6630)
*Preconditions*: in_stream != nullptr is true[.](#ops-1.sentence-1)
[2](#ops-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6634)
*Returns*: value[.](#ops-2.sentence-1)
[🔗](#lib:operator-%3e,istream_iterator)
`const T* operator->() const;
`
[3](#ops-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6645)
*Preconditions*: in_stream != nullptr is true[.](#ops-3.sentence-1)
[4](#ops-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6649)
*Returns*: addressof(value)[.](#ops-4.sentence-1)
[🔗](#lib:operator++,istream_iterator)
`istream_iterator& operator++();
`
[5](#ops-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6660)
*Preconditions*: in_stream != nullptr is true[.](#ops-5.sentence-1)
[6](#ops-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6664)
*Effects*: Equivalent to:if (!(*in_stream >> value)) in_stream = nullptr;
[7](#ops-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6672)
*Returns*: *this[.](#ops-7.sentence-1)
[🔗](#lib:operator++,istream_iterator_)
`istream_iterator operator++(int);
`
[8](#ops-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6683)
*Preconditions*: in_stream != nullptr is true[.](#ops-8.sentence-1)
[9](#ops-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6687)
*Effects*: Equivalent to:istream_iterator tmp = *this;++*this;return tmp;
[🔗](#lib:operator==,istream_iterator)
`template<class T, class charT, class traits, class Distance>
bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
const istream_iterator<T,charT,traits,Distance>& y);
`
[10](#ops-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6705)
*Returns*: x.in_stream == y.in_stream[.](#ops-10.sentence-1)
[🔗](#lib:operator==,istream_iterator_)
`friend bool operator==(const istream_iterator& i, default_sentinel_t);
`
[11](#ops-11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L6716)
*Returns*: !i.in_stream[.](#ops-11.sentence-1)