C++ Bidirectional Iterators
Iterators :
Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and assign values, and apply various operators to them in order to get the required outcome.
Bidirectional Iterators :
The Bidirectional iterator in C++ usually supports all of the characteristics of a forward iterator, and it is well-known for supporting the prefix and postfix decrement operators.
Bidirectional iterators are iterators that allow access items in both directions. To put it another way, elements may be accessed from both the end and the beginning.
A valid random access iterator is usually a valid bidirectional iterator.
Various containers, such as list, multimap, set, multiset and map, are commonly used to implement the bidirectional iterator.
Iterator and reverse iterator are two non-const iterators that are commonly known to travel in both directions offered by C++.
The forward iterator shares many of the same characteristics as Bidirectional iterator in C++; the primary difference is that the bidirectional iterator may also be decremented.
Characteristics of Bidirectional Iterator :
The attributes of a bidirectional iterator which are discussed by assuming i and j as the two iterators are as follows:
- Copying and assigning, default-constructible, and destructible are all qualities of a bidirectional iterator.
Expression :
A i;
A j(i);
J = a;
- The bidirectional iterator may be compared simply by the use of the equality or inequality operators.
Expression :
i == j
i != j
- The value of a bidirectional iterator may be accessed simply by using the dereference operator(*), or, to put it another way, it can be de-referenced.
Expression :
*i
- Dereference can be done with the mutable iterator as an Ivalue.
Expression :
*i = t
- In a bidirectional iterator, incrimination is conceivable.
Expression :
i++
++i
- The Bidirectional iterator allows for decrement also.
Expression :
i--
--i
In the pointers above, 'A' is a bidirectional type object, i and j are iterator type objects, and 't' is an entity referred by the iterator.
Functionalities of Bidirectional Iterator :
Here are some of the great features that the Bidirectional iterator has to offer.
Equality/Inequality operators :
User can compare the bidirectional iterator by using the equality (==) or inequality (!=) operator. When two iterators point to the same spot, they are considered to be equal, but only if the stated condition is met.
Dereferencing :
The bidirectional iterator could be dereferenced for both these values, such as a lvalue and a rvalue.
Incrementable :
The bidirectional iterator may be incriminated simply by using the operator (++) function.
Decrementable :
The bidirectional iterator could be decremented simply by using the operator (--) function.
Usability :
Multi-pass algorithms employ forward iterators for ease of usage (algorithms requiring the write and read operations multiple times). As a result, multi-pass algorithms can benefit from bidirectional operators.
Swappable :
The values of any two bidirectional iterators which point to separate places may simply be switched or exchanged.
Example of Bidirectional Iterator in C++ :
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int>vect1 = {10, 20, 30, 40, 50};
// Defining the iterator itr1
list<int>::iterator itr1;
// Accessing elements from the end to the beginning using the decrement operator
for (itr1=vect1.end();itr1!=vect1.begin();--itr1)
{
if (itr1 != vect1.end())
{
cout << (*itr1) << " ";
}
}
cout << (*itr1);
return 0;
}
Output :
50 40 30 20 10
Explanation :
In the above example, we showed the use of the Bidirectional iterator in C++. We created a vector vect1 with a vector array and introduced an iterator itr1. We used the decrement operator (--) to get the result we wanted. Since we started at the end of the list and worked our way back to the beginning with decrementing the pointer, the decrement operator might be used with iterators like this. We ran the loop until the iterator equalled the begin(), which is why the initial value was not displayed within the loop and had to be printed later.
Disadvantages of the Bidirectional Iterator :
The following are the constraints that are applicable to the Bidirectional Iterator in general:
- Relational operator :
Users could use an equality or inequality operator with the bidirectional iterator in general, however other iterators would not be employed on the bidirectional iterator in C++.
- Arithmetic operator :
Because the bidirectional iterator reads data in a sequential sequence, it cannot be used with an arithmetic operator.
- Offset dereference operator :
To randomly access an element, the offset dereference operator or subscript operator [] is employed, which the Bidirectional iterator in C++ does not allow.