with_index

Description

(index, it)を要素とするイテレータを返します。indexの値はstartから始まって1ずつ増加していきます。

Example

乱数で初期化した配列のうち0.5よりも小さい要素のインデックスを列挙するサンプルです。

with_indexの後にもメソッドを連ねる場合も多いと思うので、それの一例としてfilterを連ねたサンプルも同時に掲載しておきます。

C++

Xtal

array : [];
10.times{
    array.push_back(math::random());
}
array{
    it.p;
}
"-----".p;
array.with_index{|i, it|
    if (it < 0.5){
        i.p;
    }
}
"-----".p;
array.with_index.filter(|i, it| it<0.5){|i, it|
    i.p;
}

Output

0.383502
0.519416
0.830965
0.0345721
0.0534616
0.5297
0.671149
0.00769819
0.383416
0.0668422
-----
0
3
4
7
8
9
-----
0
3
4
7
8
9
reference/iterator/with_index.txt · 最終更新: 2011/07/15 09:11 by sukai
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0