Home >>C++ Standard Template Library Tutorial(STL) >C++ Bidirectional iterator

C++ Bidirectional iterator

C++ Bidirectional iterator

  • All the features of a forward iterator are generally supported by the Bidirectional iterator in C++, this iterator is generally known to support the two decrement operators that are prefix and postfix.
  • Bidirectional iterators are the iterators that are generally used to access the elements in both the directions in simple words; elements can be accessed from towards the end and towards the beginning.
  • A valid bidirectional iterator is generally a valid random access iterator.
  • The bidirectional iterator is generally implemented by various containers like list, multimap, set, multiset, map.
  • The two non-const iterators that generally known to move in both of the directions that are provided by the C++ are iterator and reverse iterator.
  • The features of the forward iterator are generally same as the Bidirectional iterator in C++, the only difference between the two is that the bidirectional iterator can also be decremented.

Properties Of Bidirectional Iterator

Here is the list of properties of the bidirectional iterator that are explained by supposing x and y as the two iterators:

Property Expressions
A Bidirectional iterator is all of these properties like copy-assignable, default-constructible, and destructible. A x;
A y(x);
Just by using the equality or inequality operator the bidirectional iterator can be compared. x==y
Bidirectional iterator’s value can be obtained just by using a dereference operator(*), in simple words, it can be de-referenced. *x
As an Ivalue the mutable iterator can be dereferenced. *x = t
Incrimination is possible in a Bidirectional iterator. x++
Decrement is possible in the Bidirectional iterator. x--

In the above mentioned table, 'A' is of bidirectional type, x and y are the objects of an iterator type, and 't' is an object that is pointed by the iterator.

Features of the Bidirectional iterator

Here are the excellent features that are provided by the Bidirectional iterator

  • Equality/Inequality operator : Users can compare the bidirectional iterator just by using an equality or inequality operator. Whenever these both iterators point towards the exact same position then it is said that these iterators are equal, only when the given condition is fulfilled.
  • Dereferencing : For both these values like as an lvalue and rvalue the bidirectional iterator can be generally dereferenced.
  • Incrementable : Incrimination of the bidirectional iterator can be done just by the use of an operator++ () function.
  • Decrementable : Decrementation of the bidirectional iterator can be done just by the use of an Operator -- () function.

Limitations Of Bidirectional Iterator

Here are the limitations that generally apply to the Bidirectional Iterator:

  • Relational operator : With the bidirectional iterator users can generally use an equality or inequality operator but the condition is that the other iterators will not be applied on the bidirectional iterator in C++.
  • Arithmetic operator : As the bidirectional iterator generally accesses the data in sequential order hence an arithmetic operator cannot be used with it.
  • Offset dereference operator : In order to random access an element randomly, offset dereference operator or subscript operator [] is used that is not supported by the Bidirectional iterator in C++.

Example of bidirectional iterator

Here is an example that will give you a brief insight in the bidirectional iterator:

#include <iostream>  
using namespace std;  
int main()  
   vector<int> vect{10,11,12,13,14}; 
   vector<int> ::iterator itr;  
   vector<int> :: reverse_iterator revitr;
   for(itr = vect.begin();itr!=vect.end();itr++)  
       cout <<*itr<<" ";  
   for(revitr = vect.rbegin();revitr!= vect.rend();revitr++)  
       cout <<*revitr<<" ";  
    return 0;  
Output :
10 11 12 13 14
14 13 12 11 10

No Sidebar ads