C++ Input Iterator - C++ Programming Tutorial
C++ Course / Miscellaneous / C++ Input Iterator

C++ Input Iterator

BLUF: Mastering C++ Input Iterator is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Input Iterator

C++ is renowned for its efficiency. Learn how C++ Input Iterator enables low-level control and high-performance computing in the tutorial below.

  • Input Iterator is an iterator used to read the values from the container.
  • Dereferencing an input iterator allows us to retrieve the value from the container.
  • It does not alter the value of a container.
  • It is a one-way iterator.
  • It can be incremented, but cannot be decremented.
  • Operators which can be used for an input iterator are increment operator(++), decrement operator(--), dereference operator(*), not equal operator(!=) and equal operator(==).
  • An input Iterator is produced by the Istream .
  • A Forward iterator, bidirectional iterator, and random access iterator are all valid input iterators.
Property Valid Expressions
An input iterator is a copy-constructible, copy-assignable and destructible. X b(a);b= a;
It can be compared by using a equality or inequality operator. a==b;a!=b;
It can be dereferenced. *a;
It can be incremented. ++a;

Where 'X' is of input iterator type while 'a' and 'b' are the objects of an iterator type.

An input iterator can be compared using an equality or inequality operator. The equality operator determines if two iterators are pointing to the same location, making them equal. In contrast, the inequality operator indicates that the iterators are not at the same location. If 'A' and 'B' are the two iterators being compared:

Example

A ==B;   // equality operator

A!=B;   // inequality operator

Let's see a simple example:

Example

#include <iostream>

#include<vector>

#include<iterator>

using namespace std;

int main()

{

    vector<int> v{1,2,3,4,5};

    vector<int>::iterator itr,itr1;

    itr=v.begin();

    itr1=v.begin()+1;

    if(itr==itr1)

    std::cout << "Both the iterators are equal" << std::endl;

    if(itr!=itr1)

    std::cout << "Both the iterators are not equal" << std::endl;

    return 0;

}

Output:

Output

Both the iterators are not equal

In the given scenario, 'itr' and 'itr1' serve as the pair of iterators, both specialized for vectors. The 'itr' iterator references the initial position within the vector while 'itr1' points to the subsequent position. Consequently, since both iterators indicate the same location, the condition 'itr1!=itr' evaluates to true, leading to the output "Both iterators are not equal."

  • Accessing the value pointed by an iterator: To access the value pointed by an iterator, we utilize the dereference operator (*). For instance, considering 'A' as an iterator:
Example

*A     //  Dereferencing 'A' iterator by using *.

Let's see a simple example:

Example

#include <iostream>

          #include<vector>

         #include<iterator>

         using namespace std;

        int main()

      {

           vector<int> v{11,22,33,44};

           vector<int>::iterator it;

           it = v.begin();

         cout<<*it;

          return 0;

}

Output:

In the given scenario, 'it' represents an iterator object referring to the initial element of a vector 'v'. Dereferencing the iterator *it retrieves the value indicated by the iterator 'it'.

  • Swappable: It is possible to interchange two iterators pointing to distinct positions.

Let's see a simple example:

Example

#include <iostream>

#include<vector>

#include<iterator>

using namespace std;

int main()

{

    vector<int> v{11,22,33,44};

    vector<int>::iterator it,it1,temp;

    it = v.begin();

    it1 = v.begin()+1;

    temp=it;

    it=it1;

    it1=temp;

    cout<<*it<<" ";

    cout<<*it1;

    return 0;

}

Output:

In the scenario mentioned, the 'it' and 'it1' iterators are interchanged by leveraging an instance of a third iterator, namely temp.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience