×

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

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 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.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.