Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Input and Output Iterators | Iterator Categories
C++ STL Iterators

bookInput and Output Iterators

Stryg for at vise menuen

Note
Definition

Input and output iterators are single-pass iterators used for sequential reading or writing, such as with streams or files. Input iterators read elements one at a time, while output iterators write them, and once advanced, they cannot be revisited.

Input and output iterators are single-pass iterators used with data sources that can only be traversed once, such as streams or devices. They move only forward, making them ideal for linear stream processing, for example reading from std::cin or writing to std::cout using iterator adaptors.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> #include <iterator> #include <vector> #include <algorithm> int main() { std::cout << "Enter integers (end with non-integer): "; std::istream_iterator<int> input_it(std::cin); std::istream_iterator<int> end_it; std::vector<int> numbers(input_it, end_it); std::cout << "Numbers entered: "; std::ostream_iterator<int> output_it(std::cout, " "); std::copy(numbers.begin(), numbers.end(), output_it); std::cout << std::endl; }

In the code above, input and output iterators demonstrate single-pass traversal with streams:

  • std::istream_iterator<int> reads integers from std::cin until a non-integer is encountered. Each value is read only once as the iterator advances.
  • The range constructor of std::vector consumes all available input from the iterator, showing that previously read values cannot be revisited.
  • std::ostream_iterator<int> writes each element to std::cout, separating values with a space.
  • Output iterators advance with each write and cannot modify or revisit already written elements.
main.cpp

main.cpp

copy
1234567891011121314151617181920212223
#include <iostream> #include <iterator> #include <sstream> int main() { std::istringstream iss("10 20 30"); std::istream_iterator<int> it(iss); std::istream_iterator<int> end; if (it != end) { std::cout << "First value: " << *it << std::endl; ++it; } // Attempt to go back to the first element (not possible) // The following line would try to dereference the advanced iterator if (it != end) std::cout << "Second value: " << *it << std::endl; // Attempting to re-read the first element is not possible // Input iterators do not support multi-pass traversal }

This example shows a key limitation of input iterators: once an element is read and the iterator is advanced, you cannot go back to it. The iterator moves forward through the stream, and previously read values cannot be revisited. This single-pass behavior defines both input and output iterators. In contrast, iterator types like forward or random access iterators support multiple passes and revisiting elements. When using input or output iterators, algorithms must be designed with the assumption that each element is accessed only once.

Note
Note

Only some STL algorithms work with input and output iterators. Algorithms that need multiple passes or element reordering usually require forward, bidirectional, or random access iterators. For example, std::copy can read from input iterators, while std::sort requires random access iterators.

question mark

What is a key limitation of input iterators?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Sektion 2. Kapitel 1
some-alt