Javatpoint Logo
Javatpoint Logo

C++ Bidirectional iterator

  • 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==y
x!=y
It can be dereferenced means we can retrieve the value by using a dereference 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 object pointed by the iterator.

Let's see a simple example:

Output:

1 2 3 4 5
5 4 3 2 1

Features of the Bidirectional iterator

C++ Bidirectional iterator
  • Equality/Inequality operator: A bidirectional iterator can be compared by using an equality or inequality operator. The two iterators are equal only when both the iterators point to the same position.

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

  • Dereferencing: A bidirectional iterator can also be dereferenced both as an lvalue and rvalue.

Suppose 'A' is an iterator and 't' is an integer variable:

  • Incrementable: A bidirectional iterator can be incremented by using an operator++() function.
  • Decrementable: A bidirectional iterator can also be decremented by using an Operator --() function.

Limitations Of Bidirectional Iterator:

  • Relational operator: An equality or inequality operator can be used with the bidirectional iterator, but the other iterators cannot be applied on the bidirectional iterator.

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

  • Arithmetic operator: An arithmetic operator cannot be used with the bidirectional iterator as it accesses the data sequentially.
  • Offset dereference operator: A Bidirectional iterator does not support the offset dereference operator or subscript operator [] for the random access of an element.

Next Topic





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA