Iteration Vs. transfer

For iteration, I know that there are different types of iterators. Forward, bidirectional, random access that can be used to access elements in an array.

For enumeration, I only heard about enumerations that can be used to index elements. But are there terms called an "enumeration" or an enumerator? If so, what is the difference between iteration and enumeration?

+4
source share
3 answers

The terminology is really language-bound and becomes quite confusing.

In C ++, "enumerat ion " ( enum ) is a name , which means "a bunch of numbered elements", so no action happens; it is just a thing (e.g. class ).

And, of course, "iterat ion " refers to an action that means "repeat the action many times." Often this is accompanied by a list of items.

But in some languages, each has a certain meaning as an action:

  • C #: Enumerators are objects that go through the elements of a collection (e.g. C ++ iterators; IEnumerator<T> ). Iterators are methods that do almost the same thing, but in the style of coroutine ( yield return ).

    C # still has enumerat ion s ( enum s), as in C ++.

  • Java: Iterators go through the elements of a collection, as in C ++. I'm not sure about the "counters".

    Java still has a counter similar to C ++, but they are also different.

  • Python: Iterators are similar to C # enumerations; generators are similar to C # iterators ( yield ).

    No enumerated ions, AFAIK.

and etc.

+11
source

I am not sure if this is the case in C ++, but in java the enumerator is essentially a faster iterator, since it is not thread safe, and therefore it has no overhead due to checking access to the stream made by the iterator .

See the blog post for more details.

0
source

In essence, C ++ SinglePassIterator, Java Iterator, and IEnumerator C # are equivalent.

AFAIK Java and C # do not offer ForwardIterator functions, probably because they must be "cloned", which is a tough operation for virtual machines. Creating a new copy in the array and accessing the index is much faster than iterators.

0
source

Source: https://habr.com/ru/post/1369411/


All Articles