C++ Bidirectional Iterator - C++ Programming Tutorial
C++ Course / Miscellaneous / C++ Bidirectional Iterator

C++ Bidirectional Iterator

BLUF: Mastering C++ Bidirectional Iterator is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Bidirectional Iterator

C++ is renowned for its efficiency. Learn how C++ Bidirectional Iterator enables low-level control and high-performance computing in the tutorial below.

  • A Bidirectional iterator supports all the features of a forward iterator, and it also supports the two decrement operators (prefix and postfix).
  • Bidirectional iterators are the iterators used to access the elements in both the directions, i.e., towards the end and towards the beginning .
  • A random access iterator is also a valid bidirectional iterator.
  • Many containers implement the bidirectional iterator such as list, set, multiset, map, multimap.
  • C++ provides two non-const iterators that move in both the directions are iterator and reverse iterator.
  • C++ Bidirectional iterator has the same features like the forward iterator, with the only difference is that the bidirectional iterator can also be decremented.
  • Properties Of Bidirectional Iterator

Suppose x and y are the two iterators :

Property Expressions
A Bidirectional iterator is a default-constructible, copy-assignable and destructible. A x;A y(x);y=x;
It can be compared by using equality or inequality operator. x==yx!=y
It can be dereferenced means we can retrieve the value by using adereference operator(*). *x
A mutable iterator can be dereferenced as an lvalue. *x = t
A Bidirectional iterator can be incremented. x++++x
A Bidirectional iterator can also be decremented. x----x

In the above table, 'A' is of bidirectional type, x and y are the objects of an iterator type, and 't' is an objeccpp tutorialed by the iterator.

Let's see a simple example:

Example

#include <iostream>

#include<iterator>

#include<vector>

using namespace std;

int main()

{

   vector<int> v{1,2,3,4,5};                       // vector declaration

   vector<int> ::iterator itr;                        // iterator declaration

   vector<int> :: reverse_iterator ritr;         // reverse iterator declaration

   for(itr = v.begin();itr!=v.end();itr++)

   {

       cout<<*itr<<" ";

   }

   cout<<'\n';

   for(ritr = v.rbegin();ritr!= v.rend();ritr++)

   {

       cout<<*ritr<<" ";

   }

    return 0;

}

Output:

Output

1 2 3 4 5

5 4 3 2 1

A bidirectional iterator allows comparison using equality or inequality operators. The iterators are considered equal only if they are pointing to the same position.

Suppose 'A' and 'B' are the two iterators:

Dereferencing: A bidirectional iterator allows accessing its value both as an lvalue and rvalue.

If 'A' functions as an iterator and 't' represents an integer variable:

A bidirectional iterator can be advanced by utilizing an operator++ method.

Example

A++;

++A;

A decrementable bidirectional iterator allows for decrementing through the use of a -- function.

Example

A--;

--A;

Limitations Of Bidirectional Iterator:

  • Comparison operator : The bidirectional iterator supports equality or inequality operators, while other iterators are not compatible with the bidirectional iterator.

Suppose 'A' and 'B' are the two iterators:

Example

A==B;           // valid

A<=B;           // invalid
  • When working with a bidirectional iterator, it is important to note that arithmetic operators are not compatible due to their sequential data access nature.
Example

A+2;              // invalid

A+1;              // invalid
  • The offset dereference operator is not supported by a Bidirectional iterator, meaning it does not allow for random access of elements using the subscript operator .

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience