TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Bidirectional Iterator

C++ Bidirectional Iterator with C++ tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ Bidirectional iterator

  • A Bidirectional iterator supports all the features of a forward iterator, and it also supports the two decrement operators (prefix and postfix).
  • Bidirectional iterators are the iterators used to access the elements in both the directions, i.e., towards the end and towards the beginning.
  • A random access iterator is also a valid bidirectional iterator.
  • Many containers implement the bidirectional iterator such as list, set, multiset, map, multimap.
  • C++ provides two non-const iterators that move in both the directions are iterator and reverse iterator.
  • C++ Bidirectional iterator has the same features like the forward iterator, with the only difference is that the bidirectional iterator can also be decremented.

Properties Of Bidirectional Iterator

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;
}

Output:

1 2 3 4 5
5 4 3 2 1

Features of the Bidirectional iterator

C++ 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 the iterators point to the same position.

Suppose 'A' and 'B' are the two iterators:

A==B;
A!=B;
  • Dereferencing: 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
  • Incrementable: A bidirectional iterator can be incremented by using an operator++() function.
A++;
++A;
  • Decrementable: A bidirectional iterator can also be decremented by using an Operator --() function.
A--;
--A;

Limitations Of Bidirectional Iterator:

  • Relational operator: 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
  • Arithmetic operator: An arithmetic operator cannot be used with the bidirectional iterator as it accesses the data sequentially.
A+2;              // invalid
A+1;              // invalid
  • Offset dereference operator: A Bidirectional iterator does not support the offset dereference operator or subscript operator [] for the random access of an element.

Next Topic




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf