What is an Iterator?

Asked

Viewed 5,293 times

12

Studying the STL in C++, I almost always come across the term iterator, example:

std::vector<int>::iterator it;

What is the function of a iterator?

2 answers

14


It is the mechanism used to "walk", element by element, by a data collection. It is an abstract and generic way of dealing with the advance between the elements of this collection. This advance can take several forms, including the opposite.

The exact functioning depends on each type of data, the important thing is that if a type has an iterator in accordance with the language every operation that iteration can be done with that object. It does not matter to him the complexity of the operation, nor how it should be done. It is an independent way of implementing access to the collection data.

He has the methods begin() and end() to indicate where iteration begins and ends.

Example:

#include <iostream>
#include <vector>
using namespace std;

int main () {
    vector<int> vetor;
    for (int i = 0; i < 10; i++) vetor.push_back(i);
    for (auto it = vetor.begin(); it != vetor.end(); it++) cout << ' ' << *it;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you are creating a type that is a collection of data, most likely you should implement an iterator for this type. There are a number of rules to be followed.

Documentation of the C iteration library++.

  • 1

    for (int it : vetor) or for (auto it : vetor) cout << ' ' << it; is valid in C++11 (an alias). https://ideone.com/Qfy5Vd Is less verbose... only ñ can control the i.

  • @Hand1cloud It’s true.

5

The iterator is also known as the cursor that provides a sequential way to access the elements of a collection without exposing its internal representation.

If you know that your List is an Arraylist, then there is no problem using the index instead of using an Iterator. For all other types (Linkedlist, Set, Map etc.) you have to use Iterator.

And anyway you keep using the for:

 for valor in sequencia:
     print(valor)

It can be used when:

  • We want to isolate the use of a data structure from its internal representation so that it can change the structure without affecting who uses it
  • For certain structures, there may be different forms of walking ("Traversal") and we want to encapsulate the exact way of walking
  • The mere substitution of an iterator allows walking in a collection of various forms

There are still two types of iterators: interno and externo.

With internal iterator, the client passes an operation to be performed by the iterator and this applies it to each element.

With external iterator (more flexible), client uses iterator interface to walk but itself (client) processes collection elements

In this case an internal iterator is only used when an external iterator is difficult to implement.

Sources:

https://en.wikipedia.org/wiki/Iterator
http://www.guj.com.br/t/qual-e-a-vantagem-do-iterator/35565

Browser other questions tagged

You are not signed in. Login or sign up in order to post.