Iterators is an actively used design pattern created in 1974.

45Years Old
  • Iterators ranks in the top 50% of entities I track
  • the Iterators wikipedia page
  • Iterators first appeared in 1974
  • I have 15 facts about Iterators. what would you like to know? email me and let me know how I can help.

Languages with Iterators include cpp, java, matlab, python, ruby, rust

Example from cpp:

std::vector<int> items;
items.push_back(5);  // Append integer value '5' to vector 'items'.
items.push_back(2);  // Append integer value '2' to vector 'items'.
items.push_back(9);  // Append integer value '9' to vector 'items'.

for (auto it = items.begin(); it != items.end(); ++it) {  // Iterate through 'items'.
  std::cout << *it;  // And print value of 'items' for current index.

Example from java:

Iterator iter = list.iterator();
//Iterator<MyType> iter = list.iterator();    in J2SE 5.0
while (iter.hasNext()) {
    if (iter.hasNext())
        System.out.print(", ");

Example from matlab:

% Define an array of integers
myArray = [1,3,5,7,11,13];

for n = myArray
   % ... do something with n
   disp(n)  % Echo integer to Command Window

Example from python:

Example from ruby:

(0...42).each do |n|
  puts n

Example from rust:

for n in 0..42 {
  println!("{}", n);

Last updated November 16th, 2019