- Iterator
- iter
1 C++[ | ]
C++
Copy
std::vector<int> items;
items.push_back(1); // Append integer value '1' to vector 'items'
items.push_back(2); // Append integer value '2' to vector 'items'
items.push_back(3); // Append integer value '3' to vector 'items'
for (std::vector<int>::iterator i = items.begin(); i != items.end(); ++i) { // Iterate through 'items'
std::cout << *i; // And print value of 'items' for current index
}
//in C++11
for(auto i:items){
std::cout << i; // And print value of 'items'
}
// 123
2 C#[ | ]
explicit version
C#
Copy
IEnumerator<MyType> iter = list.GetEnumerator();
while (iter.MoveNext())
Console.WriteLine(iter.Current);
implicit version
C#
Copy
foreach (MyType value in list)
Console.WriteLine(value);
3 Java[ | ]
Java
Copy
Iterator iter = list.iterator();
//Iterator<MyType> iter = list.iterator(); in J2SE 5.0
while (iter.hasNext()) {
System.out.print(iter.next());
if (iter.hasNext())
System.out.print(", ");
}
4 Python[ | ]
Python
Copy
s = 'abc'
it = iter(s)
print( next(it) ) # a
print( next(it) ) # b
Python
Copy
s = 'abc'
it = iter(s)
print( it.__next__() ) # a
print( it.__next__() ) # b