Bidirectional Iterators
Lesson 17 of 23 in Coddy's C++ - Standard Template Library course.
Bidirectional Iterator is an iterator that supports all of the features a forward iterator has, but it also supports the two decrement operators (pre and post). So, bidirectional iterators can be used to access and modify a sequence of elements in both directions, toward the end, as well as towards the beginning.

As we can see, the random access iterator is a valid bidirectional iterator.
So, we will learn about the features on an 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 of them are pointing to the same location.
A == B // Equality
A != B // Inequality- Dereferencing
We can dereference a bidirectional iterator to use the two functionalities, accessing the value of the element that the iterator is pointing to, as well as assigning value to the element that the iterator is pointing to.
*A = X;
X = *A;- Incrementable / Decrementable
A bidirectional iterator can be incremented using the++operator, but it can be also decremented using the--operator.
A++; // Post increment operator
++A; // Pre increment operator
A--; // Pre decrement operator
--A; // Post decrement operator- Swappable
The value of one bidirectional iterator can be exchanged or swapped with another.
We can finish the lesson by summing that the bidirectional iterator is very similar to the forward iterator, which represents the input and output iterators in one, but it is also capable of being decremented, meaning that it can access the elements in the sequence in both directions.
Challenge
EasyYou are given 10 numbers in one line from input. In the next line you are given a number X which represents the index of the number (indexes 1-10). Use a bidirectional iterator and first output the numbers starting from the index X to the end. And after that output the numbers starting from the index X going backwards to the beginning.
Input
10 20 30 40 50 60 70 80 90 100
6Output
60 70 80 90 100 60 50 40 30 20 10Try it yourself
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> numbers;
vector<int>::iterator it1, it2;
int x, y;
// Enter your code here
it1 = numbers.begin();
it1 += x - 1;
for(it1; it1 != numbers.end(); )
cout << << " ";
cout << endl;
for(it2; it2 != numbers.begin() - 1; )
cout << << " ";
return 0;
}