What are iterators and why are they needed?

I can't understand the main purpose of iterators . Googling, all the same did not understand.

Please explain:

  1. What is an iterator?
  2. Why do I need it?
  3. Advantages on the pointer
  4. How to describe it programmatically in the code? (that is, make your iterator similar to a library one).
Author: gil9red, 2013-11-10

2 answers

See. Let's say you have a container. It doesn't matter which one: map, vector, set. It contains a set of elements. And you want to refer not to the entire container, but to some place in this set of elements. So that from this place you can go forward/backward, and do something in this place: change the element, insert the element, delete the element.

How do I do this? For an array, you use an index in such cases. set inside is a red-black tree, so you'll need a reference to a node in this tree. unordered_map uses a hash table, so you need a pair of a hash index and a pointer to an element inside the bucket.

To avoid having to write algorithms specific to each container, iterators were invented.

An iterator is a data structure that "points" to some element of a container, and (for some containers) is able to jump to the previous / next element.


If you want to implement an iterator, remember, that there are different types of iterators, depending on the operations they provide. Here is a list of possible types.

If, for example, you want to implement RandomAccessIterator, you will have to define a copy constructor, assignment operator, destructor, operations ==, !=, *, ->, constructor without arguments, ++, --, +=, + (2 pcs.), -=, - (2 pcs.), <, >, <=, >=. (Other types of iterators simpler.)

In addition, you will have to specify std::iterator_traits<It>, where It is the type of your iterator.


Here's a blank for your own container on SO. Quite a lot, isn't it?

 21
Author: VladD, 2017-05-23 12:39:01

An iterator is an object that allows you to move (iterate) through the elements of a certain sequence.
The use of such objects is classified as a programming pattern Iterator.

The sequence can be either a ready-made set of objects in memory, or it can consist of objects that are created "on the fly" when moving the iterator (for example, read from a file).

In contrast to various sequences of elements (arrays, lists, files), iterators have the same interface: getting the current element, moving to the next one. This allows you to write more general algorithms that work with any iterators that support this minimal set of functions.

The C++ standard library iterators repeat the pointer interface. This allows you to use pointers to array elements as iterators. At the same time, most algorithms use only a certain minimum set of operations, for example, only moving forward. This set of operations is called the category, and the list of all iterator categories is listed here.

Options for writing your iterator are in the answers to this question.

 8
Author: Abyx, 2017-04-13 12:53:25