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; }
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
A==B; A!=B;
A bidirectional iterator can also be dereferenced both as an lvalue and rvalue.
Suppose 'A' is an iterator and 't' is an integer variable
*A = t; t = *A
A bidirectional iterator can be incremented by using an operator++() function.
A++; ++A;
A bidirectional iterator can also be decremented by using an Operator --() function.
A--; --A;
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
An arithmetic operator cannot be used with the bidirectional iterator as it accesses the data sequentially.
A+2; // invalid A+1; // invalid
A Bidirectional iterator does not support the offset dereference operator or subscript operator [] for the random access of an element.