-
迭代器
鎖定
- 中文名
- 迭代器
- 外文名
- iterator
迭代器名詞簡介
各種語言實現迭代器的方式皆不盡同,有些面嚮對象語言像Java,C#,Ruby,Python,Delphi都已將迭代器的特性內置語言當中,完美的跟語言集成,我們稱之隱式迭代器(implicit iterator),但像是C++語言本身就沒有迭代器的特色,但STL仍利用模板實現了功能強大的迭代器。STL容器的數據的內存地址可能會重新分配(reallocate),與容器綁定的迭代器仍然可以定位到重新分配後的正確的內存地址。
迭代器示例
C♯
類似於Python中使用的yield
// 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♯