blog:2023-08-02_c_note_for_iterator_迭代器
2023-08-02 C#: Note for iterator (迭代器)
-
An iterator is an object that enables users to traverse on a container object (containers, such as a linked list or array)
Designers do not need to care about the implementation details of the memory allocation of container objects when using this interface
It behaves much like a cursor in database technology, and iterators first appeared in the CLU programming language designed in 1974
An Iteration is a fancy word for a loop. An Iteration statement will perform operations a set number of times until you tell it to stop, with a true or false value.
Iteration statements also depend on an expression being evaluated until that expression evaluates to false.
This is called “loop termination criteria” and depends on Boolean values being evaluated.
C♯ iterator example
In C♯, a new form of iterator provides generators in functional language programming, using yield return
Similar to the yield used in Python
// Method that takes an iterable input (possibly an array)
// and returns all even numbers.
public static IEnumerable<int> GetEven(IEnumerable<int> numbers)
{
foreach(int i in numbers)
{
if (i % 2 == 0) yield return i;
}
}
C++ iterator example
Permalink blog/2023-08-02_c_note_for_iterator_迭代器.txt · Last modified: 2023/08/02 09:45 by
jethro