×

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

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.