In this tutorial, you will learn the implementation of the C++ iterators with an example. An iterator is an object that shows an element inside the container. You can use iterators to pass through the contents of the container. They can be view something similar to a pointer passing to some location. You can access the content at that particular location using iterators.
They play a critical role in connecting algorithms with containers along with the direction of data stored inside the containers. The common obvious form of the iterator is a pointer. A pointer can point to the elements in an array and can create iterations through them using the increment operator (++). But, they do not have similar functionality as that of pointers.
Methods Using the C++ Iterator
- Advance(): This role will help to increment an iterator position to the specified argument.
- Next(): It will declare to help the new iterator that will be shown by the iterator after incrementing the positions in the arguments.
- Previous(): This role will declare the new iterator that will be shown by the iterator after decrementing the positions in the arguments.
- Begin(): This role will declare an iterator showing the first element of the container.
- End(): it will declare an iterator showing to the past the last element of the container.
- Inserter(): It will insert the element at any position in the container.
C++ Iterators Implementation Example:
#include <iostream> #include<vector> #include<iterator> using namespace std; int main() { //Here we Declaring a Vector vector<int> v{1,2,3,4,5,6,7,8}; //Here we Declaring Iterator vector<int>::iterator i; //Here we use a Function v.insert(v.begin()+1,10); for(i=v.begin();i!=v.end();i++) { cout << *i <<" "; } return 0; }
The Types of Iterators in C++.
There are five different types of Iterators that can be classified depending on the type of functionality as shown in the flowchart below:
- Input Iterators: They are the most vulnerable of all the iterators and have very limited functionality. They can be used in single-pass algorithms. Algorithms that state the container consecutively such that no element is accessed more than once.
- Output Iterators: They are similar to input iterators, they are also very limited in their functionality and can only be used in the single-pass algorithm, but not for locating elements, but for being specified elements.
- Forward Iterator: They are essential in the hierarchy than input and output iterators and contain all the features present in these two iterators. But, as the name suggests, they also can only move in the first path, and that too one step at a time.
- Bidirectional Iterator: As the name already hints bi-directional that performs it more powerful than the above iterators. It also helps reading and writing to a container and supports the Decrement operator(–).
- Random Access Iterator. They are the Strongest iterator is the most powerful iterator as it can read, write, and can obtain randomly. Pointer-like functionality like the pointer increase and decreases.
Advantages of Iterators in C++
If you want to go from one element which iterator is currently pointing to another element that is probably y steps away from your current iterator. So, the first four in the hierarchy will get a linear value of time to make that while a random path iterator can do that in even time and that is way more interesting because that’s where time is saved. It is the most valuable feature which an Iterator provides.
Disadvantages of Iterators in C++
You can not go from one data structure to another at an equal time in some hidden way. Iterator won’t run in that case. If you are processing by a list and you forgot something. So, now if you want to go back then you can’t do that because iterators won’t work in that way. In case you want to renew the structure while traversing, you can’t do that too because of the iterative way of storing its position.