std::istrea...eam_iterator(3)
β¨ NAME β¨
std::istream_iterator::istream_iterator β the constructor for std::istream_iterator, aka the glow-up moment where your iterator gets its act together π
πΈ Synopsis πΈ
istream_iterator(); (until C++11)
constexpr istream_iterator(); (since C++11)
constexpr istream_iterator( (2) (since C++20)
std::default_sentinel_t );
istream_iterator( istream_type& stream ); (1) (3)
istream_iterator( const istream_iterator& other
(until C++11)
); (4)
istream_iterator( const istream_iterator& other
(since C++11)
) = default;
π Description π
okay so here's the tea on each overload, bestie βοΈ
1,2)β constructs the end-of-stream iterator, and value-initializes the stored value. This constructor isconstexprif the initializer in the definitionauto x = T();is a constant initializer (since C++11). no cap, it's giving "I'm literally the end, periodt" π3)β initializes the iterator, stores the address ofstreamin a data member, and performs the first read from the input stream to initialize the cached value data member. it's the main character constructor fr fr π₯4)β constructs a copy ofother. Ifstd::is_trivially_copy_constructible<T>::valueistrue, this copy constructor is a trivial copy constructor (since C++11). slay, lowkey efficient π¦
π§ Parameters π§
streamβ stream to initialize theistream_iteratorwith. the source of the vibes, iykyk πotherβ anotheristream_iteratorof the same type. your iterator bestie π«Ά
π Example π
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#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
literally iconic output, 10/10, ate and left no crumbs π
β Defect reports β
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. not me reading the patch notes like it's drama π
- LWG 1280 β applied to C++98 β behavior as published: overload
(3)storedstreamdirectly β correct behavior: stores its address instead πΈ - P0738R2 β applied to C++98 β behavior as published: the first read might be deferred to the first dereference β correct behavior: always performed in the constructor π₯
bestie if you made it this far you're officially a C++ scholar, no cap ππ
source: http://cppreference.com Β· version: 2024.06.10 Β· that's the whole std::istream_iterator(3) moment, periodt π¦β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms