Decrementing an iterator in C++

In this tutorial, we are going to learn how to decrement an iterator in C++. These are used to traverse through the elements of the containers.

All containers in C++ do not support all iterators. We can only decrement random access iterators and bidirectional iterators. But we cannot decrement forward iterators, input iterators, and output iterators.

Decrementing a random access iterator:

#include<bits/stdc++.h>
using namespace std;
int main(){
  vector<int> ::iterator itr;
  //itr is random access iterator
  vector<int> a={1,2,3,4};
  itr=a.begin();
  //Itertor points to first element
  itr++;
  //Iterator points to the second element
  itr--;
  /* Iterator points to the first element 
   after decrementing */
  cout<<*itr<<endl;
  itr = itr + 1;
  // Iterator points to the second element
  itr = itr - 1;
  /*Iterator points to the first element
    after decrementing it using arithmetic operation*/
  cout<<*itr<<endl;
}

Output:

1
1

In the above code, we used a vector. So if we declare an iterator for a vector it will be a random access iterator.

The random access iterator can be decremented either by using the decrement operator(- -) or by using arithmetic operation.

If itr is an iterator. We can use itr – – or – – itr for decrementing it. As it is a random access iterator we can also use arithmetic operation such as itr=itr-1 for decrementing it.

Decrementing a bidirectional iterator:

#include<bits/stdc++.h>
using namespace std;
int main(){
  set<int> ::iterator itr;
  //itr is bidirectional iterator
  set<int> a={1,2,3,4};
  itr=a.begin();
  //Itertor points to first element
  itr++;
  //Iterator points to the second element
  itr--;
  /* Iterator points to the first element 
   after decrementing */
  cout<<*itr<<endl;
}

Output:

1

If we declare an iterator for a set it will be a bidirectional iterator.

We can decrement a bidirectional only by using the decrement operator(- -). We cannot decrement it using arithmetic operations.

If itr is an iterator. We can use itr – – or – – itr for decrementing it. We cannot use itr=itr -1 for decrementing the iterator.

Decrementing an iterator using a built-in function:

We can use the advance() function for decrementing an iterator.

#include<bits/stdc++.h>
using namespace std;
int main(){
  vector<int> ::iterator itr;
  vector<int> a={1,2,3,4};
  itr=a.begin();
  //Itertor points to first element
  itr++;
  //Iterator points to the second element
  advance(itr,-1);
  /* Iterator points to the first element 
   after decrementing */
  cout<<*itr<<endl;
}

Output:

1

Traversing a container in reverse order in C++:

If a container supports a random access iterator or a bidirectional iterator then we can traverse it in a reverse order using an iterator.

#include<bits/stdc++.h>
using namespace std;
int main(){
  vector<int> ::iterator itr;
  vector<int> a={1,2,3,4};
  itr = a.end()-1;
    //Iterator points to the last element
    while(true){
        cout<<*itr<<' ';
        /* If the iterator reaches the first
           element we can break the loop */
        if(itr == a.begin()){
            break;
        }
        itr--;
    }
    cout<<endl;
}

Output:

4 3 2 1

 

We hope that you have got a clear idea of how to decrement an iterator in C++.

Also read: How to check if two given sets are disjoint in C++

Leave a Reply

Your email address will not be published. Required fields are marked *