Input Iterators in C++

Last Updated : 30 Jun, 2026

Input iterators are one of the five iterator categories in the C++ Standard Library and provide the minimum functionality required to traverse a sequence. They are primarily used for sequential reading of elements from containers and input streams.

  • Support single-pass, forward-only traversal of elements in a sequence.
  • Provide read-only access to elements without allowing modification.
C++
#include <iostream> 
#include <vector> 
using namespace std; 
int main() { 
    vector<int> v = {10, 20, 30, 40}; 
    auto it = v.begin(); 
    
    while (it != v.end()) { 
        cout << *it << " "; 
        ++it; 
        
    } 
    
    return 0; 
    
}

Output
10 20 30 40 

Explanation: The iterator it traverses the vector sequentially. It accesses each element using the dereference operator (*) and moves to the next element using the increment operator (++).

Iterator Hierarchy

Input iterators are the base category in the iterator hierarchy. The following iterator categories also satisfy the requirements of input iterators:

This means that any iterator belonging to these categories can also be used wherever an input iterator is required.

Features of Input Iterators

Input iterators provide the following capabilities:

1. Single-Pass Traversal

Input iterators can be used only with single-pass algorithms, where each element is accessed at most once.

Examples of such algorithms include:

2. Equality and Inequality Comparison

Input iterators support equality comparisons.

A == B
A != B

Two iterators are equal only if they point to the same position.

3. Dereferencing

Input iterators support dereferencing to access the value they point to.

*A
A->member

The dereferenced value can only be used for reading.

4. Increment Operation

Input iterators can move only in the forward direction.

++A
A++

5. Swappable Values

The values referenced by input iterators can participate in swap operations.

Practical Usage of Input Iterators

Input iterators are commonly used in algorithms that only need to access elements sequentially.

Example: std::find()

The std::find algorithm only requires reading and traversing elements.

C++
template <class InputIterator, class T> 
InputIterator find(InputIterator first, 
                    InputIterator last, 
                    const T& val) 
{ 
    while (first != last) { 
        if (*first == val) 
        return first; 
        ++first; 
        
    } 
    return last; 
    
}

Explanation: The iterator is only dereferenced and incremented, which makes input iterators suitable for this algorithm.

Example: std::copy()

The std::copy() algorithm uses input iterators only for the source range.

C++
template<class InputIterator, class OutputIterator> 
OutputIterator copy(InputIterator first, 
                    InputIterator last, 
                    OutputIterator result) 
{ 
    while (first != last) 
        *result++ = *first++; 
        
    return result; 
    
}

Explanation: The source iterator only needs read access, so an input iterator is sufficient. The destination iterator requires write access, so an output iterator is used.

Limitations of Input Iterators

Input iterators have several limitations that make them the weakest iterator category.

1. Read-Only Access

Input iterators cannot be used to modify elements.

*it = 10; // Not allowed

2. No Backward Traversal

Input iterators only move forward.

--it; // Not allowed

3. Cannot Be Used in Multi-Pass Algorithms

Since input iterators support only single-pass traversal, they cannot be used in algorithms that require revisiting elements multiple times.

4. No Relational Operators

Input iterators do not support relational comparisons.

A == B; // Allowed
A <= B; // Not allowed

5. No Arithmetic Operations

Input iterators do not support arithmetic operations.

A + 1; // Not allowed
A - 2; // Not allowed

Example of Input Iterator Operations

CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> v1 = { 1, 2, 3, 4, 5 };

    // Declaring an iterator
    vector<int>::iterator i1;

    for (i1 = v1.begin(); i1 != v1.end(); ++i1) {
        // Accessing elements using iterator
        cout << (*i1) << " ";
    }
    return 0;
}

Output
1 2 3 4 5 

Explanation: The iterator traverses the container sequentially and accesses each element using the dereference operator.

Applications of Input Iterators

Input iterators are commonly used in:

  • Sequential searching algorithms.
  • Counting operations.
  • Reading elements from streams.
  • Single-pass traversal algorithms.
Comment