![]() |
VOOZH | about |
An iterator is an object like a pointer that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them.
Example:
1
Explanation: The above example demonstrates accessing the first element of a vector container by first obtaining its iterator (it). Then dereference this iterator with the asterisk (*) operator, which outputs the value 1.
cType::iterator iteratorName;
where,
We can use autokeyword to skip to write container type because auto keyword automatically detected container type itself.
auto iteratorName;
All iterators do not have similar functionality, depending upon the functionality of iterators they can be classified into five categories:
Iterator | Description |
|---|---|
It is a read only iterator but not modified the value using input iterator. | |
Output iterator is used to assign the values. | |
It is the combination of input and output iterator means for both access and assigning the values. | |
This iterator can move in both direction either in forward or backward. Alos it's have all functionalities of forward iterators. | |
Random-access iterators are iterators that can be used to access the element at the distance from the element they pointed to. And it's had all functionalities of bidirectional iterators. |
Not all of these iterators are supported by all containers in STL. Different containers support different iterators, like vectors support random-access iterators, while lists support bidirectional iterators. The whole list is as given below:
| CONTAINER | TYPES OF ITERATOR SUPPORTED |
|---|---|
| Vector | Random-Access |
| List | Bidirectional |
| Deque | Random-Access |
| Map | Bidirectional |
| Multimap | Bidirectional |
| Set | Bidirectional |
| Multiset | Bidirectional |
| Stack | No iterator Supported |
| Queue | No iterator Supported |
| Priority-Queue | No iterator Supported |
The following diagram shows the difference in their functionality with respect to various operations that they can perform.
| ITERATORS | ACCESS | READ | WRITE | ITERATE | COMPARE |
|---|---|---|---|---|---|
| Input | -> | =*i | ++ | ==, != | |
| Output | *i= | ++ | |||
| Forward | -> | =*i | *i= | ++ | ==, != |
| Bidirectional | =*i | *i= | ++, -- | ==, != | |
| Random-Access | ->, [ ] | =*i | *i= | ++, --, +=, -=, ==, +, - | ==, !=, <, >, <=, >= |
To know more, refer to the article - Iterators in C++
The main differences between Iterators and Pointers are given in the below table:
| Feature | Iterators | Pointers |
|---|---|---|
| Definition | Abstractions that allow traversal over containers. | Memory addresses that store the location of another variable. |
| Usage | Used with STL containers (e.g., vector, list, map). | Used for direct memory manipulation. |
| Flexibility | Can work with different types of containers. | Only works with raw memory and arrays. |
| Operations | Support operations like increment (++), decrement (--), and dereferencing (*). | Can perform arithmetic operations (+, -, ++, --). |
| Safety | Safer, as they provide bounds checking in STL containers. | Prone to segmentation faults due to out-of-bounds access. |
| Container Compatibility | Can be used with STL containers efficiently. | Works with raw arrays and dynamically allocated memory. |
| Performance | Slightly slower than pointers due to additional overhead. | Faster as they directly access memory. |
There are following advantages of iterators listed below: