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:
#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:
Features of the 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:
A==B; // valid
A<=B; // invalid
- Arithmetic operator: An arithmetic operator cannot be used with the bidirectional iterator as it accesses the data sequentially.
A+2; // invalid
A+1; // invalid
- Offset dereference operator: A Bidirectional iterator does not support the offset dereference operator or subscript operator [] for the random access of an element.
|