×

Input Iterators in C++

What are input iterators?

Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented.

All the iterators like forward, input, and bidirectional are also regarded as valid input iterators.

Input operators are not confined to only one type. There are multiple types. Let’s discuss this in detail.

Input iterators are the simplest and the weakest iterators. Such iterators are simple to use and valid for other functionalities that can be achieved by using them in our programs. They work on the phenomenon of iterating sequential inputs where each value is pointed by the iterator. Input iterators are read only once and later incremented.

To understand more clearly, consider these points that are easy to remember about input iterators:

  1. Input Iterators are used to read values from the container.
  • Values in containers can be accessed by using dereferencing in the input container.
  • It fails to change container values.
  • It is uni-directional (one-way).
  • It is incremental and cannot be decreased.
  • Operators for an input iterators include increment operator (++), decrement operator (--), dereference operator (*), not equal operator (!=) and equal operator (==).
  • An input Iterator is produced by the Istream.
  • A bidirectional iterator, forward iterator, and all others are input iterators.

Let us look at the operations and properties of input iterators.

       Properties   Expressions
Copy-assignable, copy-constructible and destructible  X b(a); b=a
  
Can be dereferenced      *x;
  
Can be compared with equality and inequality operator   a==b;  ,  a!=b
  
Can be incremented       ++i;
  

After considering the operations and properties, let us now closely examine the features of input iterators with the help of coding examples below.

Features of Input iterators

Let us look at a coding example to clearly visualize the working of input iterators. Let’s consider an example.

Example Code:

#include <iostream> 
 #include<vector> 
 #include<iterator> 
 using namespace std; 
 int main() 
 
     vector<int> vect{10,20,30,40,50}; 
     vector<int>::iterator itr1,itr2; 
     itr1=vect.begin(); 
     itr2=vect.begin()+1; 
     if(itr1==itr2)
       {
     std::cout << "Both the iterators are equal" << std::endl; 
     }
       if(itr1!=itr2)
       {    
     std::cout << "Both the iterators are not equal" << std::endl; 
     }
       return 0; 
 }   

1. Input iterators can only be used in those algorithms which allow single pass. A single-pass algorithm can go to all locations in a range when we may search any element in a container. It can iterate through all the locations at least once.

Input iterators are used to compare for equality with other iterators. We know that two iterators cannot point to a same position. But if they do, it clearly gives us a result that both the iterators are equal.
So, the following two expressions are valid if X and Y are input iterators: 

For Example:

X == Y  // Checking for equality
X != Y  // Checking for inequality

3. Input iterator can be used for dereferencing using * and -> operators for obtaining the value for storing the value that is being pointed by the iterator.

To visualize this, let us look at a coding example.

#include <iostream> 
#include<vector> 
  #include<iterator> 
  using namespace std; 
 int main() 
   {
    vector<int> vect{10,20,30,40}; 
    vector<int>::iterator itr; 
    itr = vect.begin(); 
    cout<<*itr; 
   return 0;
 }   

Output:

10

Explanation: Here, we can see that the vector is using begin() function. But it has not been assigned end() functional, since it is not an input iterator. The output 10 gives the idea that the *itr(dereferencing operator) points only the first element in the vector(dynamic array). So it prints only 10.

  • An input iterator is swappable provided two inputs pointing at different locations.

Let us discover it with a coding example

#include<iostream>
 #include<iterator> 
 using namespace std; 
 int main() 
 
     vector<int> vect{10,20,30,40}; 
     vector<int>::iterator itr,itr1,temp; 
     itr = vect.begin(); 
     itr1 = vect.begin()+1; 
     temp=itr; 
     itr=itr1; 
     itr1=temp; 
     cout<<*itr<<" "; 
     cout<<*itr1; 
     return 0; 
  

Output:

20 10

Limitations of input iterators

  1. Input iterators are restricted to only increment. Decrementing is not allowed while using input iterator. 

If X is used as an input iterator, then

X--   // Not allowed

2. Since it is unidirectional and is restricted to move only move forward, therefore, such iterators cannot be used in multi-pass algorithms, in which we need to process the container multiple times. 

3. Input iterators can be used with equality operators (==) but cannot be used as relational operators like <=.

x == y     // Allowed
x <= y     // Not Allowed

4. Input iterators resemble relational operators since they cannot be used with arithmetic operators like addition(+) and subtraction(-). This clearly implies that input iterators can move strictly forward and also they move one by one or sequentially.

If x and x are taken as input iterators, then

x+ 1     // Not allowed
y - 3     // Not allowed

Related Topics

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.