FreeBSD Manual Pages
std::istream_iterator(3) C++ Standard Libary std::istream_iterator(3) NAME std::istream_iterator - std::istream_iterator Synopsis Defined in header <iterator> template< class T, class CharT = char, class Traits = std::char_traits<CharT>, (until C++17) class Distance = std::ptrdiff_t > class istream_iterator: public std::iterator<std::input_itera- tor_tag, T, Distance, const T*, const T&> template< class T, class CharT = char, class Traits = std::char_traits<CharT>, (since C++17) class Distance = std::ptrdiff_t > class istream_iterator; 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 con- structed, by calling the appropriate operator>>. The actual read operation is performed when the iterator is incremented, not when it is dereferenced. The first ob- ject is read when the iterator is constructed. 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. Dereferenc- ing 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. T must meet the DefaultConstructible, CopyConstructible, and CopyAs- signable requirements. Member types Member type Definition iterator_category std::input_iterator_tag value_type T difference_type Distance pointer const T* reference const T& char_type CharT traits_type Traits istream_type std::basic_istream<CharT, Traits> Member types iterator_category, value_type, difference_type, pointer and reference are required to be obtained by inheriting from (until C++17) std::iterator<std::input_iterator_tag, T, Distance, const T*, const T&>. Member functions constructor constructs a new istream_iterator (public member function) destructor destructs an istream_iterator, including the cached value (public member function) operator* returns the current element operator-> (public member function) operator++ advances the iterator operator++(int) (public member function) Non-member functions operator== compares two istream_iterators operator!= (function template) (removed in C++20) Notes When reading characters, std::istream_iterator skips whitespace by default (unless disabled with std::noskipws or equivalent), while std::istream- buf_iterator does not. In addition, std::istreambuf_iterator is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character. Example // Run this code #include <iostream> #include <sstream> #include <iterator> #include <numeric> #include <algorithm> int main() { std::istringstream str("0.1 0.2 0.3 0.4"); std::partial_sum(std::istream_iterator<double>(str), std::istream_iterator<double>(), std::ostream_iterator<double>(std::cout, " ")); std::istringstream str2("1 3 5 7 8 9 10"); auto it = std::find_if(std::istream_iterator<int>(str2), std::istream_iterator<int>(), [](int i){return i%2 == 0;}); if (it != std::istream_iterator<int>()) std::cout << "\nThe first even number is " << *it << ".\n"; //" 9 10" left in the stream } Output: 0.1 0.3 0.6 1 The first even number is 8. Defect reports The following behavior-changing defect reports were applied retroac- tively to previously published C++ standards. DR Applied to Behavior as published Cor- rect behavior P0738R2 C++98 the first read may be deferred to the the first read is performed first dereferencing in the con- structor See also ostream_iterator output iterator that writes to std::basic_os- tream (class template) istreambuf_iterator input iterator that reads from std::ba- sic_streambuf (class template) http://cppreference.com 2022.07.31 std::istream_iterator(3)
NAME | Synopsis | Member types | Member functions | Non-member functions | Notes | Example | Output: | See also
Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::istream_iterator&sektion=3&manpath=FreeBSD+Ports+15.0>
