how to iterate on Array?
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 27 11:32:10 PDT 2015
On 06/27/2015 10:43 AM, aki wrote:
> void pr(Array!int a) {
> foreach(i, v; a[]) {
You are rightly assuming that the loop counter is available for all
container types. Unfortunately, it is the case only for arrays.
Luckily, it is very easy to achieve it with std.algorithm.enumerate:
import std.stdio;
import std.container;
import std.range;
void pr(Array!int a) {
foreach(i, v; a[].enumerate) {
writeln("%4s: %s\n", i, v);
}
}
void main()
{
auto a = Array!int();
pr(a);
}
Ali
More information about the Digitalmars-d-learn
mailing list