Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iteration Vs. Enumeration

Tags:

c++

For iteration, I know there are different types of iterators E.g. Forward, Bidirectional, Random Access that can be used to access items in an array.

For enumeration, I've only heard about enums that can be used to index items. But are there terms called 'enumeration' or enumerator? If so then, What's the difference between iteration and enumeration?

like image 209
Jason Avatar asked Dec 21 '22 09:12

Jason


1 Answers

The terminology is really tied to the language, and gets pretty confusing.

In C++, an "enumerat​ion" (enum) is a name, meaning "a bunch of numbered items", so there's no action going on; it's just a thing (like a class).

And of course, "iterat​ion" refers to an action, meaning "to repeat an action many times". Often, this is accompanied by going through a list of items.

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

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

    C# still has enumerat​ion​s (enums) like in C++.

  • Java: Iterators go through the items of a collection, like in C++. I'm not sure about "enumerators".

    Java still has enumerat​ion​s similar to C++, but they're sort of different as well.

  • Python: Iterators are like C# enumerators; generators are like C# iterators (yield).

    No enumerat​ion​s, AFAIK.

etc.

like image 118
user541686 Avatar answered Dec 24 '22 00:12

user541686