×

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

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

How to Setup Environment for C++ Programming on Mac

Mac OS X code Installation There are so many environments available for C++. We are going to install jGrasp and Xcode in our mac operating system. Instruction for installation of jGrasp and...

2 minutes read.

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages. C++ Function A function is a set of statements that executes a task....

5 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 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++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

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

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

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

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute 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.