×

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.

Related Topics

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.